Unix shell 文件复制扁平化文件夹结构 [英] Unix shell file copy flattening folder structure
问题描述
在 UNIX bash shell(特别是 Mac OS X Leopard)上,将每个具有特定扩展名的文件从文件夹层次结构(包括子目录)复制到同一目标文件夹(没有子文件夹)的最简单方法是什么?
On the UNIX bash shell (specifically Mac OS X Leopard) what would be the simplest way to copy every file having a specific extension from a folder hierarchy (including subdirectories) to the same destination folder (without subfolders)?
显然存在源层次结构中存在重复项的问题.如果它们被覆盖,我不介意.
Obviously there is the problem of having duplicates in the source hierarchy. I wouldn't mind if they are overwritten.
示例:我需要复制以下层次结构中的每个 .txt 文件
Example: I need to copy every .txt file in the following hierarchy
/foo/a.txt
/foo/x.jpg
/foo/bar/a.txt
/foo/bar/c.jpg
/foo/bar/b.txt
到名为dest"的文件夹并获取:
To a folder named 'dest' and get:
/dest/a.txt
/dest/b.txt
推荐答案
在 bash 中:
find /foo -iname '*.txt' -exec cp \{\} /dest/ \;
find
会查找/foo
路径下所有匹配通配符*.txt
的文件,不区分大小写(这就是-iname
的意思).对于每个文件,find
将执行 cp {}/dest/
,用找到的文件代替 {}
.
find
will find all the files under the path /foo
matching the wildcard *.txt
, case insensitively (That's what -iname
means). For each file, find
will execute cp {} /dest/
, with the found file in place of {}
.
这篇关于Unix shell 文件复制扁平化文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!