2012-07-10 18 views

回答

5

如果你有bash的V4和在.profile有

shopt -s globstar 

,你可以使用:

mv ./sourcedir/**/*.ext ./targetdir 
+0

這也適用於zsh的。事實上,它已經因爲我記得(像過去十年?),在默認配置下只要我可以告訴工作。 – 2012-07-10 17:05:45

2

使用查找和一個簡單的while循環對子級做到這一點:

find directory -name '*.foo'|while read file; do 
    mv $file other_directory/ 
done 

這將移動的所有文件與.foo後綴來other_directory/

5
$ find <directory> -name '*.foo' -exec mv '{}' <other_directory> \; 

find通過目錄並遞歸搜索結構並對其找到的符合搜索條件的任何文件執行給定操作。

在這種情況下,-name '*.foo'是搜索條件,並-exec mv '{}' <other_directory> \;告訴find上發現的任何文件,其中'{}'被轉換爲文件名和\;表示命令的結束執行mv

+0

能否請您解釋一下? – lovespeed 2012-07-10 17:01:31

+0

這是怎麼說的呢? – 2012-07-10 17:03:00

+0

我認爲需要有一些循環,這將去了所有的子目錄。我是bash腳本編程的總新手 – lovespeed 2012-07-10 17:08:23

1

您可以使用找到xargs的以減少循環或多次調用需要MV

find /path/to/files -type f -iname \*foo -print0 | 
    xargs -0 -I{} mv {} /path/to/other/dir 
相關問題