Linux查找删除文件夹中的文件

目标是find具有给定名称的目录,并删除其中的所有文件,保持实际的目录

find /home/www/sites/ -iname '_cache' -exec du -hs {} \; 

这给了我一个文件的大小列表

 204K /home/www/sites/test.site.com/html/development/Temporary/_cache 904K /home/www/sites/test.site2.com/html/development/Temporary/_cache 

用Linux find命令可以实现吗?

我尝试了一些东西,看起来是在工作,这是Alex在这里发布的一个类似的解决scheme。

 find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -exec rm {} \; 

它应该只删除在_cache目录中find的文件,只能在这个目录下。 它不会从_cache目录的子目录中删除任何文件。

当然,在使用之前试用它,而不是使用rm,放在那里或什么是无害的。

我还没有彻底地testing这个逻辑…但是你可以在一个循环里面做一些事情:

 for findname in $(find /path/to/search -name '_pattern') do find $findname -type f done 

因此,您可以获得与您的search模式相匹配的文件列表,然后使用新search循环查找要删除的文件。

写的方式会给你一个文件列表,所以你可以redirect到一个文件,然后通过与rm循环。 你也可以在for循环里追加一个exec来findw /。 我肯定会推荐以书面forms运行testing逻辑,并确保比赛看起来不错。

正确的命令擦除它

 find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -delete 

find一个删除选项。 这将删除任何匹配。

从手册页

-删除

  Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit stat- us will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option. Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully use -prune and -delete together.