Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert dos2unix line endings for all files in a directory

Tags:

Currently, I am using the following command to change dos2unix line endings, but this is for file-y file...

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

Is there a way to run this command for all the files in a directory?

like image 901
user1934146 Avatar asked Jan 17 '13 05:01

user1934146


People also ask

How do you change line endings for all files in VS Code?

VS Code => Settings => Files: EoL And choose “\n” as EoL character if you would like your files to have Unix Style line endings and choose “\r\n” if you would like your files to have Windows Style line endings. VS Code — Eol Settings for Files. That settings applies to all new files that you create.

How do I convert dos2unix?

Option 1: Converting DOS to UNIX with dos2unix Command The simplest way to convert line breaks in a text file is to use the dos2unix tool. The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name.

How do I change the end of line from CR LF to LF?

Once you select View > Show Symbol > Show End of Line you can see the CR LF characters visually. You can then use the menu item Edit > EOL Conversion and select Unix (LF). After selection Edit > EOL Conversion > Unix (LF) your file will be correct for submission.


2 Answers

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.

like image 83
Bryson Avatar answered Sep 19 '22 12:09

Bryson


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.

like image 43
Hubbitus Avatar answered Sep 19 '22 12:09

Hubbitus