find and copy file to another directory..

Hi Everybody,

i want a samll help to write a script.
i had source location with :/user/bin (bin contains subdirectories with like names emails etc and had several files in each subdirectory)

and target location with :/usr/scripts (having same subdirectories names and had some files)

Now i have to write a script that copies the files in to the source subdirectory to target directory by putting a timestamp infront of the file name (like 20081022_saomename.txt) .

If i passed the directory name as argument to the script it should search in the respective directory and copy files from source to the respective target direcoty with timestamp file name.
otherwise if called the script it must copy all the subdirectories and files to target Location.

Thanks in advance.

hey Reddy482,

you can use something like:

#!/bin/bash

if [ "$#" -eq 2 ]
then
        SRC="$1"
        DES="$2"

        for file in `ls $SRC`
        do
              PREDT=`date +%Y%m%d`;
                /bin/cp -fRar $SRC/$file $DES/$PREDT\_$file;
        done
else
        echo "Two arguments are necessary"
fi

you can make it better by adding checks for actual directory etc, but this is the basic for what you need, I've tested and it works great.

run it like:

./copy-files.sh /home/source /home/destination

Let me know if this helped.