bashsymlinkrsynccp

A shell script question involving rsync and symlink using cp


The script under consideration has been coded as shown below:

#!/bin/bash

#recursive remove

REC_RM="rm -rf"

#recursive copy

REC_CP="cp -Rf"

#rsync

RSYNC_CMD="rsync -av --progress"

#recursive link

REC_LN="cp -as"

#the script excepts three arguments as follows:
#
# a filename such as files.txt with entries along the lines of
#
# [HEADER]
# <PATH>
#  
# the name of the specific HEADER to look for in the file, such as "FILES"
#
# a destination path such as /dst
#
# so the script should be invoked as <script> <filename> <header string> <destination path>
#
# for the sake of brevity, the segment related to argument checking has been omitted

#get the path listed under the specific header from within filename

SRC=$(grep -A1 $2 $1)
   
SRC=$(echo ${SRC} | awk '{print $2}')

#remove any preceding '/' from the path

FWD_SLSH='/'

SRC_MOD=$(echo ${SRC} | sed "s|^$FWD_SLSH\(.*\)|\1|")

#append the modified source path to the destination path

DST=${3}${SRC_MOD}

#create the destination directory path

mkdir -p ${DST}

#sync the source and destination paths

eval "$RSYNC_CMD" ${SRC} ${DST}

#recursively purge source path

eval "$REC_RM" ${SRC}

#recursively link destination path back to source path by means of copy

eval "$REC_LN" ${DST} ${SRC}

The file /src_vinod/test/data.txt and the directory /dst_vinod are created outside of the script.

Now assuming that the filename files.txt contained the following entry:

[FILES]
/src_vinod/test/

And the script were to be invoked as shown below (the name script.sh has been used):

script.sh filenames.txt FILES /dst_vinod/

After execution, I would expect the destination path to be populated as follows:

/dst_vinod/src_vinod/test/data.txt

and the source path to be populated as

/src_vinod/test/data.txt

where data.txt is a soft link to the file data.txt under the destination path

However, the result I get is as follows:

the destination path consists of

/dst_vinod/src_vinod/test/test/data.txt

and the source path consists of

/src_vinod/test/test/data.txt

where data.txt is a soft link to the file data.txt under the destination path

I am not sure why the directory test/ is replicated.

Any thoughts?

TIA

Vinod


Solution

  • How to use rsync to copy files

    Note: Specifying “/” after the source directory only copies the contents of the directory. If we do not specify the “/”after the source directory, the source directory will also be copied to the destination directory.

    This was the issue in my script. I fixed this issue and it started working as expected.