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?
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.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With