Writte a script to copy the files older than 7 days using find and cp

Hi
I'm trying to writte a script (crontab) to copy files from one location to another... this is what i have:

find . -name "VPN_CALLRECORD_20130422*" | xargs cp "{}" /home/sysadm/patrick_temp/

but that is not working this is the ouput:

cp: Target ./VPN_CALLRECORD_20130422111500_20130422113000_136777.csv must be a directory
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn

I'm not sure what is wrong, i'm using SOLARIS 10.

Do you have any idea of what is wrong ?

Don't think xargs takes {} like that.

find . -name "VPN_CALLRECORD_20130422*" | while read FILE
do
        echo cp "$FILE" /path/to/destination
done

Remove the echo once you've tested and are sure it does what you want.

1 Like

use the -exec option to find.

find . -type f -name "VPN_CALLRECORD_20130422*" -exec cp -v "{}" /home/sysadm/patrick_temp/ \;
1 Like
find . -type f -name "VPN_
CALLRECORD_20130422*" -exec cp "{}" /home/sysadm/
patrick_temp/ +

-exec handles
special file names well.
+ can be faster than \;

Probably true but that option is not portable.

Hi all, my thanks to Corona688 and frank_rizzo for your reply, both scripts worked successfully , but the one of frank_rizzo worked removing "-v" because it was triggering "invalid option". Thanks guys.

The one of MadeInGermany didn't work because of this:
root@tefpebe01> find . -type f -name "VPN_CALLRECORD_20130502*" -exec cp "{}" /home/sysadm/patrick_temp/ +
find: incomplete statement

anyway many thanks because the intention of help, i forgot to tell you that i'm working on solaris 10, maybe there is a difference .