Convert linefeeds for all files in a folder (CRLF to LF)

If you are working in teams using different operating systems, it could happen, that you get files with unsuitable linefeeds in it. For me as a Linux user the bad guys are normally CRLF’s (Carriage Return and Line Feed @see https://en.wikipedia.org/wiki/Newline).

To just convert the CRLF’s in a single file to LF you can use the following command:

The inverse operation that converts LF’s to CRLF’s is:

The following code snippet shows how to convert linefeeds for all files in a folder:

What does this command do?

  1. searches for all files (-type f) in the current folder (.)
  2. pushes the filenames into standard output (with a tailing null character -> no Problems with whitespace or even linebreaks in filename)
  3. which is piped into xargs that is informed about the terminating null character (-0) => so knows how to interpret the input properly
  4. xargs only reads the first given argument (here: the filename) from the input (-n 1)
  5. xargs pushed the filename  to dos2unix that converts every file given
  6. The -P 4 – parameter tells xagrs to spread the different file conversion commands over at maximum four processes. Which is good to process a huge amount of files.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.