Linux 如何在整个目录上运行 dos2unix?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11929461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:04:05  来源:igfitidea点击:

How can I run dos2unix on an entire directory?

linuxdos2unix

提问by Vivek Gaur

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

我必须使用dos2unix. 我无法弄清楚如何做到这一点。

采纳答案by CyberDem0n

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

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

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

将递归查找当前目录中的所有文件并调用这些文件 dos2unix 命令

回答by Summer_More_More_Tea

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

如果没有子目录也可以

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

回答by nikc

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.

这将一次传递 1 个文件,并使用 4 个处理器。

回答by Mathias Dolidon

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 :

由于我对 dos2unix 不太满意,所以我推出了自己的简单实用程序。除了速度和可预测性方面的一些优势外,语法也更简单一些:

endlines unix *

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

如果你想让它进入子目录(跳过隐藏的目录和非文本文件):

endlines unix -r .

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

endlines可在此处获得https://github.com/mdolidon/endlines

回答by Brett

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

对于任何 Solaris 用户(我使用的是 5.10,也可能适用于较新版本以及其他 unix 系统):

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:

dos2unix 不会默认覆盖文件,它只会将更新版本打印到标准输出,因此您必须指定源和目标,即两次相同的名称:

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

回答by Kyle Strand

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

最好跳过隐藏的文件和文件夹,例如.git.So 而不是使用find,如果您的bash版本足够新或者您正在使用zsh,请执行以下操作:

dos2unix **

Note that for Bash, this will require:

请注意,对于 Bash,这将需要:

shopt -s globstar

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

....但这是一个足够有用的功能,你应该诚实地把它放在你的.bashrc任何地方。

If you don'twant 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 onlyhidden entries:

如果你希望跳过隐藏的文件和文件夹,但你还是不想惹find(和我也不会怪你),你可以提供第二递归水珠参数匹配隐藏的条目:

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.

请注意,在这两种情况下,glob 都会扩展以包含目录,因此您将看到以下警告(可能会多次出现): Skipping <dir>, not a regular file.

回答by rafapedroche

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

回答by friederbluemle

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

一个常见的用例似乎是标准化提交到 Git 存储库的所有文件的行尾

git ls-files | xargs dos2unix

Keep in mind that certain files (e.g. *.sln, *.bat) etc are onlyused on Windows operating systems and should keepthe CRLFending:

请记住,某些文件(例如*.sln*.bat)等都是使用在Windows操作系统中,应该保持CRLF结尾:

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

If necessary, use .gitattributes

如有必要,请使用 .gitattributes

回答by Strabek

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

我遇到了同样的问题,多亏了这里的帖子,我已经解决了。我知道我有大约一百个文件,我只需要为 *.js 文件运行它。 find . -type f -name '*.js' -print0 | xargs -0 dos2unix

Thank you all for your help.

谢谢大家的帮助。

回答by Sahand

I think the simplest way is:

我认为最简单的方法是:

dos2unix $(find . -type f)