Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help.
dest=path/to/destination
cd /path/to/source
for file in $( ls )
do
if [ -d $fname ] then
continue
fi
if [ -f $dest/$fname ] ; then
cp $fname $dest/${fname}1
else
cp $fname $dest/${fname}
fi
done
Thanks Jim. Definitely appreciated.
Bill
Jim, just for my curiosity, where is the $fname variable getting its values? From the $(ls) output? Why are you checking -d then continue if it is a file name?
Also why do you need the { } on the copy command? Is that to put the variable value and then 1 as opposed to look for $fname1?