2011-08-17 45 views

回答

39

RM的男子網頁顯示:

-r, -R, --recursive 
      remove directories and their contents recursively 

這意味着標誌-r期待一個目錄。 但*.xml不是一個目錄。

如果你想刪除從下面遞歸當前目錄下的所有.xml文件是命令:

find . -name "*.xml" -type f|xargs rm -f 
+4

不需要調用xargs;使用'find -exec'。 – paulmelnikow

+1

@noa ..我知道...有很多方法可以在unix中做類似的事情。它由OP來選擇使用哪一個。我剛纔給出了我經常使用的東西。 – Vijay

+6

@noa其實'xargs'更有效率。 '-exec'爲每個匹配的文件調用'rm',而'xargs'分批執行。當存在大量文件時,這可能會產生很大的不同。 – suvayu

24

我假設你想遞歸地刪除所有*.xml文件(在當前和所有子目錄內)。要做到這一點,使用find

find . -name "*.xml" -exec rm {} \; 

在一個側面說明,遞歸缺失讓我害怕。在我的理智的日子裏,我往往會先與該步驟:

find . -name "*.xml" 

(不-exec位)只是爲了看看什麼可能採取的飛躍之前被刪除。我建議你也這樣做。你的文件會感謝你。

+0

是想要做幾種不同的類型。但是你的腳本似乎沒有在Bash內工作。 – Will

+0

輸出是什麼? – paulmelnikow

+0

你不需要轉義'{}' - 原始版本很好--OP可能只是錯誤地輸入了它。 –

2

更華麗的方式,雖然這是一個在UNIX系統支持較少:

rm -rf */*.xml 

這將從當前目錄的所有子目錄中刪除xml文件。

+0

這將工作的目錄樹比深度只有2級? –

3

閱讀this answer on finding empty directories unix,我只是瞭解了-delete行動:

-delete 
      Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit status will be nonzero (when it even‐ 
      tually 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 start‐ 
      ing 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. 

來源:人找到

這意味着,你也可以遞歸刪除所有XML的文件是這樣的:

find . -name "*.xml" -type f -delete 
0

一個簡單的方法是

RM -f * .XML

這將刪除當前目錄下的所有.xml文件。

0

ZSH遞歸通配來拯救!

調用的zsh: zsh

確保您在DIR是你打算在: cd wherever

列表第一: ls **/*.xml

刪除: rm **/*.xml

我會抵制強烈的誘惑,在bash和ju st指向主題here的相關zsh文檔。

相關問題