簡體   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