Need help in backup script

Hello Everyone,

I have written shell script to copy some file from a text file.

while read Eachline
do cp -p $Eachline /usr/x/x/x/temp_part_bkp/
done < /usr/x/x/x/list.txt

but main problem is suppose that there are two files with same name
for eg: in list.txt
/usr/x/x/x/test.atr and /usr/x/x/x1/test.atr
then its overwritten by other one :frowning:
I need a script which can copy both file in some location
like

/usr/x/x/x/test.atr in path /usr/x/x/x/temp_part_bkp/
and /usr/x/x/x1/test.atr in path /usr/x/x/x/temp_part_bkp1/

can we achive it. please help I am stuck :confused:

You could perform a check to see if the file exists

while [ -x /usr/x/x/x/temp_part_bkp/$Eachline ] ; do
   $Eachline="$Eachline.1"
done;
1 Like

With GNU cp, you can use

cp --backup=t

Thanks but can you please tell me more how to do that i am trying to implement that logic.....pls help

A funny eval, ( not tested) :

while read file
do
   ll $file
   var=$(basename ${file})
   eval let ${var}=${var}+1
   eval t=\$$var
   if [ $t -gt 1 ]
   then
      cp -p $file /usr/x/x/x/temp_part_bkp${t}
   else
     cp -p $file /usr/x/x/x/temp_part_bkp/
   fi
done</usr/x/x/x/list.txt