Linux - 递归浏览目录?

时间:2014-10-15 16:18:33

标签: linux directory

假设我在目录/ home / videos中,并希望以递归方式遍历其下的所有目录。如果目录名称包含“images”,我想删除目录及其所有内容。此外,这可以为文件完成吗?让我们说在每个目录中遍历每个文件并检查其名称是否以“.mp3”结尾并删除它?

由于

2 个答案:

答案 0 :(得分:1)

find . -name "*images*" -type d -exec rm -r {} \;

find . -name "*.mp3" -type f -exec rm -rf {} \;

-exec rm -rf {} \; :删除与文件模式匹配的所有文件。

-type f:仅匹配文件,不包含目录名称。

-type d:仅匹配目录名

答案 1 :(得分:1)

这是一个很好的教程。 http://www.cyberciti.biz/faq/linux-unix-how-to-find-and-remove-files/

所以你可以做点什么

find /home/videos -type d -name "*images*" -exec rm -rf {} \;

find /home/videos -type f -name "*.mp3" -exec rm -rf {} \;