我试图删除所有隐藏的目录和文件,然后递归删除它们。当前目录级别的唯一隐藏目录是.
和..
,但是几个目录中的每个目录都有几个隐藏文件(以._
开头)。 ls -aR | egrep '^\.\w+' |
会列出我想要的所有文件,但添加' | xargs' rm'`给我rm错误"没有这样的文件或目录"。
我认为这意味着我尝试删除的每个目录都需要附加它的父目录和/
。但也许我错了。
如何更新此命令以删除这些文件?
答案 0 :(得分:5)
使用find
:
find . -type f -name .\* -exec rm -rf {} \;
-exec
是空白安全的:{}
会将文件路径(相对于.
)作为单个参数传递给rm
。
更好的是:
find . -name .\* -delete
(感谢@ 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
这是因为它将它在白色空间上的输入分开以提取文件名。我们可以使用另一个分隔符;来自man find
:
-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