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:
but it doesn't retain each files' directory.
What's the proper command?
find /otrs_attachment_files/2013/10/15/ -type f \( -name "*.png" -o -name "*.jpg" \) -exec cp -ar {} /root/otrs_test_filebackup \;
but it doesn't retain each files' directory.
What's the proper command?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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