用途find:
find . -type f -name .\* -exec rm -rf {} \;
Run Code Online (Sandbox Code Playgroud)
的-exec是空白安全的:{}将通过文件路径(相对于.)作为单个参数来rm.
更好的是:
find . -name .\* -delete
Run Code Online (Sandbox Code Playgroud)
(感谢@ John1024).第一种形式为找到的每个文件生成一个进程,而第二种形式则没有.
xargs 默认情况下不是白色空间安全:
$ touch a\ b
$ find . -maxdepth 1 -name a\ \* | xargs rm
rm: cannot remove ‘./a’: No such file or directory
rm: cannot remove ‘b’: No such file or directory
Run Code Online (Sandbox Code Playgroud)
这是因为它将它在白色空间上的输入分开以提取文件名.我们可以使用另一个分隔符; 来自man find:
Run Code Online (Sandbox Code Playgroud)-print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by pro? grams that process the find output. This option corresponds to the -0 option of xargs.
所以:
find . -type f -name .\* -print0 | xargs -0 rm
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |