使用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
甚至'-exec RM -rf {} \;' – fedorqui
@fedorqui是這就是我想要的使用太;-) – Kenney
謝謝。即在當前目錄中查找隱藏的目錄,但不在可見目錄中隱藏文件。 – 1252748