
如何從目錄中遞歸刪除大小小於 1MB 的檔案?
答案1
這可以透過以下方式完成find
:
find . -type f -size -1M -exec rm {} +
請注意,這將遞歸地下降到子目錄,並將無條件刪除所有小於 1 MB 的檔案。當心。
答案2
這應該可以完成這項工作:
$ find <directory> -type f -size -1M -delete
答案3
只是為了多樣性和可能的(可能是邊際的)性能增益:
find <directory> -type f -size -1M -print0 | xargs -0 rm
答案4
您可以查看此鏈接http://ayaz.wordpress.com/2008/02/05/bash-quickly-deleting-empty-files-in-a-directory/,它正是您想要的。
for file in *;
do
file_size=$(du $file | awk '{print $1}');
if [ $file_size == 0 ]; then
echo "Deleting empty file $file with file size $file_size!";
echo "rm -f $file";
fi;
done
您可以使用 for 迴圈遍歷所有文件,然後使用 du 和 awk 來尋找文件大小,如上面的範例所示。