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"
doneBut its coping all the files in srcdir. Please can anyone help me in this.
02 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