moving files and creating a link to it

Hi All,

I am in a tricky situation where I have to move my files to a different mount point and create a link in place of the file which will point to the moved location.

to explain you in details:-

say I have two mount points

/dir/mount1/
/dir/mount2/

I have my application files in /dir/mount1/application/

in /dir/mount1/application/ i have directories like

/dir/mount1/application/month
/dir/mount1/application/month/day
/dir/mount1/application/month/day/hour
/dir/mount1/application/month/day/hour/min1/a.txt
/dir/mount1/application/month/day/hour/min1/b.txt
/dir/mount1/application/month/day/hour/min1/c.txt

and

/dir/mount1/application/month/day/hour/min2/d.txt
/dir/mount1/application/month/day/hour/min2/e.txt

like wise....these files will be created in day-hour-min format directories...
now I need to move these .txt files to
/dir/mount2/application/ directory which will have exact directory structure which mount point 1 has.

and in place of original files in mount1 I will create softlinks which will be linked to the shifted files respectively....

any idea will be appreciated.... :slight_smile:

---------- Post updated 10-24-09 at 12:39 AM ---------- Previous update was 10-23-09 at 11:18 PM ----------

any luck with any one?

Hi.

As a starting point:

cd /dir/mount1
tar cf - application | (cd ../mount2; tar xf -)
find application -type f | while read FILE; do
   rm "$FILE"
   ln -s ../mount2/"$FILE" "$FILE"
done

Thanks Scott for suggestion...I had to discard the tar methos as if I do tar for the first time I will get all the files in my back up location but.....as on my files gets loaded in primary directory I have to move those files also...then how do I move the differential tar to back up location and create link?

Try this...

mount1="/dir/mount1"
mount2="/dir/mount2"

cd $mount1

f_list=`find application -type f`

tar cf - $f_list | (cd $mount2; tar xf -)

for i in $f_list ; do
  rm $i
  ln -s $mount2/$i $i
done

thanks for suggestion but req is diff here...I have to move the files daily through a cron script.....your suggestion will work if I do once....

but If i do daily then I have to copy the files(and not the links which are created already)...so If I do tar and move the files in my new location I will get error as directory already exist....:frowning:

I am working on a shell script whcih will create the exact directory structire in the new location, move the files and create a link to it....

by the time any new suggestions are appreciated....:slight_smile:

The script will only move files that are not links already. The tar pipe is a convenient way to move the files without having to create logic to make paths and directories that may not exits. What the script does:

  1. Create variables for directories
  2. create a list of regular files. Note sym links will not be in the list.
  3. Use a tar pipe to transfer the files and directory structure to the destination directory. Only files that have not already been processed are moved.
  4. remove the source files and create sym links in the source directory to the target directory

For the first run, every file will be moved and linked. For any subsequent runs, only files that are not sym links (i.e. new additions) will be moved.