Shell script to copy a specific file from one folder to another with date

I want to write a script which look for a specific file in a specific folder. file name and location will be pre-define in the script. A loop will run, if the file exist, it will copy that file to another location with date on it(this location will also be define), and if the file does not exist, it will give a pop up with "File does not exist" I have tried with this:

srcdir="/home/bmsc/HOME/oam/data"
dstdir="/home/bmsc/backup"
source=/home/bmsc/HOME/data/haguard.xml
destination=/home/bmsc/backup/
d=$(date +%m%d%y)
for srcfile in ${srcdir}/*
do dstfile=$(basename $srcfile) dstfile=${dstfile/\./${d}\.} cp "$source" "$dstdir/$dstfile"
done

But its coping all the files in srcdir. Please can anyone help me in this.

0

2 Answers

If you're looking for a specific file, why run a loop? You can directly test for the existence of that file:

if [[ -f $source ]]
then dstfile=$(basename "$source") ...
fi
4
srcdir="/home/bmsc/HOME/oam/data"
dstdir="/home/bmsc/backup"
source=/home/bmsc/HOME/data/haguard.xml
destination=/home/bmsc/backup/
d=$(date +%m%d%y)
if [ -f $source]; then dstfile=$(basename $source) dstfile=${dstfile/\./${d}\.} cp "$source" "$dstdir/$dstfile"
fi

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like