We help IT Professionals succeed at work.

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

meade470
meade470 asked
on
116 Views
Last Modified: 2015-09-25
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?
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT

Commented:
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

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.