Ale*_*man 24 command-line files
有没有办法应用该dos2unix
命令,以便它针对文件夹及其子文件夹中的所有文件运行?
man dos2unix
没有显示任何-r
或类似的选项可以让这一切变得直截了当?
使用bash
:
shopt -s globstar
dos2unix **
Run Code Online (Sandbox Code Playgroud)
globstar
shell 选项允许bash
使用 glob **
。这与路径名类似*
,但匹配/
路径名(因此也匹配子目录中的名称)。这适用于子目录中包含适量文件(不是数千个)的目录。
在zsh
和yash
shell 中(使用set -o extended-glob
in yash
),你会做
dos2unix **/*
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 find 来查找要通过 dos2unix 命令运行的目录结构中的所有文件
find /path/to/the/files -type f -exec dos2unix {} \;
Run Code Online (Sandbox Code Playgroud)
查看 find 的手册页,有很多选项可用于指定评估的内容
跳过二进制文件和隐藏文件对我来说很重要:
这个对我来说效果很好:
find . -type f -not -path '*/\.*' -exec grep -Il '.' {} \; | xargs -d '\n' -L 1 dos2unix -k
Run Code Online (Sandbox Code Playgroud)
转换为:在当前目录中递归查找所有非隐藏文件,然后使用 grep,列出所有非二进制 (-I) 非空文件,然后将其通过管道传输到 xargs(由换行符分隔)一个文件中dos2unix 并保留原始时间戳。
也可以看看:
https://github.com/mdolidon/endlines