Link to home
Start Free TrialLog in
Avatar of meade470
meade470

asked on

How to find certain files and copy them with their directories to a directory?

We are trying to find all files in a directory, that contains multiple sub-directories for the file type ".png" and ".jpg". We are using:

find /otrs_attachment_files/2013/10/15/ -type f \( -name "*.png" -o -name "*.jpg" \) -exec cp -ar {} /root/otrs_test_filebackup \;

Open in new window


but it doesn't retain each files' directory.

What's the proper command?
ASKER CERTIFIED SOLUTION
Avatar of ThomasMcA2
ThomasMcA2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of serialband
serialband
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skullnobrains
skullnobrains

if you don't want / can't use rsync or your version of cp does not handle --parents

find ... | cpio -p TARGETDIR

if you don't have cpio, many archieving tools including tar and pax (likely available) can be used, but that would actually create a temorary archieve with a possibly large performance hit

cd TARGETDIR ; find ... | tar -c -f- | tar -x -f-

some versions of xargs can replace tokens several times ( and you can add -P for parallel processing ) but this spawns one command per file

find ... | xargs -J% cp % TARGETDIR/%

likewise in shell scripting (yes the double-double quotes work)

find ... | while read path
do   mkdir -p "`dirname "TARGETDIR/$path"`"
        cp "$path" "TARGETDIR/$path"
done
I just noticed that I forgot the source directory in my arguments.  Here's the corrected command.
rsync -am --include='*.png' --include='*.jpg' --include='*/' --exclude='*'  /otrs_attachment_files/2013/10/15/ /root/otrs_test_filebackup

Open in new window