2012-07-10 72 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 
相关问题