我有这样的目录结构
/home
/dir-1
some-file.php
/dir-2
sibling.php
target-file.php
/dir-3
/dir-4
other-sibling.php
sibling.php
target-file.php
/dir-5
target-file.php我需要针对所有包含文件"target-file.php“的目录,并删除这些目录及其内容。在我的结构中,最终想要的结果是:
/home
/dir-1
some-file.php
/dir-3我正在努力:
rm -rf /home/*/target-file.php但它只是删除该文件(target-file.php),而不是删除兄弟姐妹或父目录。
请帮帮忙
发布于 2022-03-21 03:08:26
用这个:
#!/bin/bash
find . -type f -name target-file.php -print0 | while IFS= read -r -d '' line
do
echo "$line"
/bin/rm -fr "$(dirname "$line")"
done像这样使用files.
dirname使用find的find . -type f -name target-file.php -print来查看files.dirname的列表删除文件名,所以只剩下names./bin/rm -fr删除directories.echo行的目录,这只是显示正在处理的文件/目录)。https://stackoverflow.com/questions/71543497
复制相似问题