find & copy files with absolute path

hi all,
can i get script find file & copy that file with path
for an example
sourse : /home/abc/
destination : /home/backup/
files which need to find : tmp*
copy these files with its absolute path inside
like :- /home/abc/x/y/z/tmp.txt to /home/backup/date/x/y/z/tmp.txt

thanks in advance

The customary tool for this would be tar.

cd /home/abc
test -d /home/backup/date || mkdir /home/backup/date
find -type f -name 'tmp*' -print |
tar cf - |
( cd /home/backup/date ; tar xf - )

You usually want to run this as root in order to preserve permissions etc.

rsync is also nice and efficient if you have that.

thanks for reply
hey can we do this using cp command.
i tried like this but it prints error
test=$(find /home/user/ -name test* -print0)
for i in $test; do cp -a $i /home/user/tmp/; done

whts wrong in my script

please help me ASAP.

It is not clear that cp is the right tool for what you are trying to do.

You can try it with find and cpio, something like:

cd /home/abc
find tmp* -print | cpio -pvdmu /home/backup

Check the manpages for the used options.

Regards

thanks for reply
this script create folder named as "tmp" in tmp folder it does not find & copy files starts with "tmp*"

what i want script search the file "tmp" in abc & its subfolder & copy to destination

From my first reply:

find . -type f -name 'tmp*' -print | ...

thanks for reply
i tried this also but fail.
can u please give whole script.

Have you try to find out why it fails?
Put some effort into solving the problem with the given examples instead of always asking for the right answers.

Regards

thanks for reply

cd /home/jagannath.n
dated=$(date +%d-%m-%y)
test -d /home/jagannath.n/tmp/$dated | mkdir /home/jagannath.n/tmp/$dated
file=$(find . -name test* -print)
cp -rf $file /home/jagannath.n/tmp/$dated

my problem is solved using above script but it print below error .

cp: cannot stat `./Azureus': No such file or directory
cp: cannot stat `Downloads/SHELL': No such file or directory
cp: cannot stat `SCRIPTING': No such file or directory
cp: cannot stat `IN': No such file or directory
cp: cannot stat `LINUX/VTC': No such file or directory
cp: cannot stat `-': No such file or directory
cp: cannot stat `UNIX': No such file or directory
cp: cannot stat `SHELL': No such file or directory
cp: cannot stat `SCRIPTING': No such file or directory
cp: cannot stat `ADVANCED/Course': No such file or directory
cp: cannot stat `Files/Chapter': No such file or directory

is it error?

It's because you don't

[quote the arguments properly]
(UNIX Shell Quotes - a simple tutorial). But just adding double quotes around "$file" doesn't really help, either, because it's not a single file. I'd still recommend using tar or cpio for this.

Note there are two |:s before the mkdir, it's an "or"; "a || b" is a shorthand for "if not a then b" (which is equivalent to "a or b").

try this

#! /usr/bin/ksh

cd /home/backup/
find /home/abc/ -name "*.s" | while read line
do
  p=$(dirname $line )
  p1=$(echo $p | sed 's/\///')
  mkdir -p $p1
  cd $p1
  tocopy=$(pwd)
  echo $tocopy
  cp $line $tocopy
  cd -
done

That will still not cope with file names with spaces in them. Proper quoting, please!!

Era now it will copoy files with spaces also

#! /usr/bin/ksh

cd /home/backup/
find /home/abc/ -name "*.s" | while read line
do
  p=$(dirname "$line" )
  p1=$(echo $p | sed 's/\///')
  mkdir -p $p1
  cd $p1
  tocopy=$(pwd)
  echo $tocopy
  cp "$line" $tocopy
  cd -
done

Thanks you
thanks era & aju_kup
My problem is solved