convert dos2unix line endings for all files in a directory

dos2unix *.txt

works fine to convert all *.txt files in a directory... no need for the 'find' command.


Simple:

sed -i 's/\r//' filename1 filename2 … filenameN

or, if you are speaking about content of one directory

sed -i 's/\r//' *

Work just well for me.


The simplest way to run dos2unix against an entire directory recursively is to just have the find command execute it for each file it finds based on your criteria.

For example, to find all files (excluding directory names) in your current directory and all its sub-directories and have dos2unix do the default conversion on each of those files:

find . -type f -exec dos2unix -k -s -o {} ';'

This will output every single filename, but not necessarily change each of them. This works as follows:

find .: Find anything in this directory, including its subdirectories, and anything in those subdirectories as well (recursion)
-type f: Only return 'regular file' names. Exclude folder names from the results.
-exec: Execute the following command for every result. Everything beyond this point should be treated as part of that command until the ; character is found.
dos2unix: dos2unix will be executed with the following options...
-k: Keep the date stamp of the output file the same as the input file
-s: Skip binary files (images, archives, etc.). This option is included by default, but I use it anyway in case that default were different on some systems (e.g. OS X v. Debian v. CentOS v. Ubuntu v. ...).
-o: Write the changes directly to the file, rather than creating a new file with the data in the new format.
{}: This tells find to insert the filename it has found as a parameter of the dos2unix call.
';': Tell find that the params for dos2unix have ended. Anything beyond this point will again be treated as a parameter of find.

Tags:

Awk

Sed