How can I run dos2unix on an entire directory?

2019-01-20 21:21发布

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

7条回答
女痞
2楼-- · 2019-01-20 21:21

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

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

查看更多
老娘就宠你
3楼-- · 2019-01-20 21:28

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 {} {} \;
查看更多
狗以群分
4楼-- · 2019-01-20 21:28

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

ls | xargs -I {} dos2unix "{}"
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-20 21:30

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

查看更多
别忘想泡老子
6楼-- · 2019-01-20 21:30
for FILE in /var/www/html/files/*
do
 /usr/bin/dos2unix FILE
done
查看更多
Ridiculous、
7楼-- · 2019-01-20 21:41

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.

查看更多
登录 后发表回答