Copy nested folders contents to one folder recursively (terminal)

风流意气都作罢 提交于 2019-12-20 08:41:36

问题


I have a Wordpress upload folder that is structured using subfolders for months.

wolfr2:uploads wolfr$ tree .
.
|-- 2007
|   |-- 08
|   |   |-- beautifulkatamari.jpg
|   |   |-- beautifulkatamari.thumbnail.jpg
|   |   |-- beetle.jpg
|   |   |-- beetle.thumbnail.jpg

How do I use terminal to copy all the images recursively into another folder? I can't seem to wildcard folders like you can wildcard filenames. (e.g. *.jpg or *) (I'm on Mac OSX)

cp -R ./*.jpg .

?


回答1:


This will copy all *.jpg files from the current folder to a new folder and preserve the directory structure.

tar cvfp `find . -name "*.jpg"` | (cd <newfolder>; tar xfp -)

To copy without preserving the directory structure:

cp `find . -name "*.jpg"` <newfolder>



回答2:


Off the top of my head:

find . -type f -name \*.jpg -exec cp \{\} $TARGETFOLDER \;

If that doesn't work, comment and I'll try again, but find is definitely the way to go.



来源:https://stackoverflow.com/questions/1936187/copy-nested-folders-contents-to-one-folder-recursively-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!