LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copying jpegs recursively while preserving the directory structure (https://www.linuxquestions.org/questions/linux-newbie-8/copying-jpegs-recursively-while-preserving-the-directory-structure-825181/)

jsmith54 08-09-2010 06:47 PM

Copying jpegs recursively while preserving the directory structure
 
So I have a bunch of directories:

dir1
dir2
dir3
etc.

which themselves all contain subdirectories:

dir1
subdir1
subdir2
etc.
and at the lowest level they contain all of these jpegs that I need. The problem is that I only need some of them. They're named like this:

pic1.jpg
pic1_med.jpg
pic1_small.jpg
pic2.jpg
pic2_med.jpg
etc.

I want to just grab the ones without the size suffix and copy them all to another set of folders, while preserving the directory structure. The numbering all starts at 1 for each low level subdirectory, so I think that the directory structure is the only way to not get them mixed up.

I know that cp has a recursive option -r but how do I just extract the ones without the underscore? And then how do I preserve the directory structure when I move them over?

GrapefruiTgirl 08-09-2010 07:17 PM

find
 
Code:

find . -type f -regextype posix-extended -regex ".*pic[0-9]+\.jpg" -exec cp -p --parents "{}" /TARGET/PATH/ \;
The above should work; run it from the top of the source tree - it will recursively copy the pictures matching filename of "pic" followed by "number(s)" ending with ".jpg", with no underscore/extension, and create the target directory tree as the source was.

I tested it - and it works for me - but as with anything free, no guarantee!

Cheers,
Sasha

PS - I encourage you to read the manpages for `cp` and `find` and understand the options I used.


All times are GMT -5. The time now is 03:57 AM.