How can I run dos2unix on an entire directory?

LinuxDos2unix

Linux Problem Overview


I have to convert an entire directory using dos2unix. I am not able to figure out how to do this.

Linux Solutions


Solution 1 - Linux

find . -type f -print0 | xargs -0 dos2unix

Will recursively find all files inside current directory and call for these files dos2unix command

Solution 2 - Linux

If it's a large directory you may want to consider running with multiple processors:

find . -type f -print0 | xargs -0 -n 1 -P 4 dos2unix 

This will pass 1 file at a time, and use 4 processors.

Solution 3 - Linux

As I happened to be poorly satisfied by dos2unix, I rolled out my own simple utility. Apart of a few advantages in speed and predictability, the syntax is also a bit simpler :

endlines unix *

And if you want it to go down into subdirectories (skipping hidden dirs and non-text files) :

endlines unix -r .

endlines is available here https://github.com/mdolidon/endlines

Solution 4 - Linux

It's probably best to skip hidden files and folders, such as .git. So instead of using find, if your bash version is recent enough or if you're using zsh, just do:

dos2unix **

Note that for Bash, this will require:

shopt -s globstar

....but this is a useful enough feature that you should honestly just put it in your .bashrc anyway.

If you don't want to skip hidden files and folders, but you still don't want to mess with find (and I wouldn't blame you), you can provide a second recursive-glob argument to match only hidden entries:

dos2unix ** **/.*

Note that in both cases, the glob will expand to include directories, so you will see the following warning (potentially many times over): Skipping <dir>, not a regular file.

Solution 5 - Linux

A common use case appears to be to standardize line endings for all files committed to a Git repository:

git ls-files -z | xargs -0 dos2unix

Keep in mind that certain files (e.g. *.sln, *.bat) are only used on Windows operating systems and should keep the CRLF ending:

git ls-files -z '*.sln' '*.bat' | xargs -0 unix2dos

If necessary, use .gitattributes

Solution 6 - Linux

For any Solaris users (am using 5.10, may apply to newer versions too, as well as other unix systems):

dos2unix doesn't default to overwriting the file, it will just print the updated version to stdout, so you will have to specify the source and target, i.e. the same name twice:

find . -type f -exec dos2unix {} {} \;

Solution 7 - Linux

I think the simplest way is:

dos2unix $(find . -type f)

Solution 8 - Linux

I have had the same problem and thanks to the posts here I have solved it. I knew that I have around a hundred files and I needed to run it for *.js files only. find . -type f -name '*.js' -print0 | xargs -0 dos2unix

Thank you all for your help.

Solution 9 - Linux

I've googled this like a million times, so my solution is to just put this bash function in your environment.

.bashrc or .profile or whatever

dos2unixd() {
  find $1 -type f -print0 | xargs -0 dos2unix
}

Usage

$ dos2unixd ./somepath

This way you still have the original command dos2unix and it's easy to remember this one dos2unixd.

Solution 10 - Linux

for FILE in /var/www/html/files/*
do
 /usr/bin/dos2unix FILE
done

Solution 11 - Linux

If there is no sub-directory, you can also take

ls | xargs -I {} dos2unix "{}"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivek GaurView Question on Stackoverflow
Solution 1 - LinuxCyberDem0nView Answer on Stackoverflow
Solution 2 - LinuxSoothView Answer on Stackoverflow
Solution 3 - LinuxMathias DolidonView Answer on Stackoverflow
Solution 4 - LinuxKyle StrandView Answer on Stackoverflow
Solution 5 - LinuxfriederbluemleView Answer on Stackoverflow
Solution 6 - LinuxBrettView Answer on Stackoverflow
Solution 7 - LinuxSahandView Answer on Stackoverflow
Solution 8 - LinuxStrabekView Answer on Stackoverflow
Solution 9 - LinuxBen WindingView Answer on Stackoverflow
Solution 10 - LinuxrafapedrocheView Answer on Stackoverflow
Solution 11 - LinuxSummer_More_More_TeaView Answer on Stackoverflow