script to move

Hi,

Want to write a script which move like:
these are files in different dir structure,

/home/darshak/test/sh/new.txt
/home/darshak/test/new1.txt
/home/darshak/test/pl/file.txt

script will move same structure as above but in different dir,

/usr/local/test/sh/new.txt
/usr/local/test/new1.txt
/usr/local/test/pl/file.txt

I am finding files by below command which I have to move:
find /home/darshak -name "*" -mtime +4

Also was looking for tar-ing option but the data is about 20-30 GB so will take lot of time to tar and untar. So dropped the idea.

If you can guarantee that the target directory structure already exists (i.e. moving new files into existing directories only), then all you need is:

find /home/darshak -mtime +4 | sed -e 's=.*=mv & _&=' -e 's=_/home/darshak=/usr/local=' | sh

However, if the target directory structure cannot be guaranteed, the solution is a little longer:

#!/bin/sh
for file in `find /home/darshak -mtime +4`; do
    newfile=`echo $file | sed 's=/home/darshak=/usr/local='`
    newdir=`dirname $newfile`
    [ ! -d $newdir ] && mkdir -p $newdir
    mv $file $newdir/.
done

If you can use cp command first with the "--parents" parameter then you can achieve with ease what you what, then try the find command again then delete them.

find /home/darshak -name "*" -mtime +4 -exec cp {} /path/to/destination --parents \;

then

find /home/darshak -name "*" -mtime +4 -exec rm -rf {} \;

This will be a nasty solution but it will work.