繁体   English   中英

如何查找具有特定名称的所有子目录并删除其内容(而不是删除目录本身)

[英]How to find all subdirectories with a specific name and delete its contents (and NOT delete the directories themselves)

我似乎找不到能在类似问题的几篇文章中找到解决方案的解决方案。 这是我用来测试文件夹的最接近的命令:

find . -iname "*Adobe Premiere Pro Video Previews*" -exec sh -c 'rm -rf {}/*' \;

问题是find . -iname "*Adobe Premiere Pro Video Previews*" find . -iname "*Adobe Premiere Pro Video Previews*"本身会找到子目录并打印它们,而-exec sh -c 'rm -rf {}/*' \\; 完成仅删除内容而不删除目录本身的工作。 但是它们不能用于查找目录并在放在一起时删除其内容。 我应该使用什么命令来同时完成这两项任务?

谢谢

#!/bin/bash
while read -r line; do
    echo "Deleting CONTENTS of folder: $line"
    rm -rf "$line/*"
done <<< $(find . -type d -iname "*Adobe Premiere Pro Video Previews*")

这会遍历find的结果(使用-type d仅显示目录,因此,如果文件包含搜索字符串,则不会出现问题),并对每个结果的内容执行rm -rf /*很重要,因为没有它,它也会删除结果目录,而不仅仅是其内容。

for dir in `find . -type d -iname "*Adobe Premiere Pro Video Previews*"`; do
    find $dir -type f -delete
done

我可能不是bash方面的专家,但是对我来说,以下命令正在运行:

find . -iname "*test*" -type d -exec sh -c "rm -rf {}/*" \\;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM