man cp
Code:
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
pseudo code
Code:
if [[ -f filename.bak ]] ; then
cp source destanation/filename(1).bak
fi
but it is a little more complicated if you already have a filename(1).bak ...
http://www.tldp.org/LDP/abs/html/fto.html
you could use this. Just modify it to your needs:
This function searches the directory and finds where I left off at. returning that highest number to a file I added a numbering system like you're talking of. filename(1).bak filename(2).bak etc
Code:
#!/bin/bash
countnum="$(find /home/userx/testscript/lost -name "*.mp3" | wc -l )"
FILES="$(cd /home/userx/testscript/lost/ && ls *.mp3)"
MAXNUM="$(find ~/testscript/lost -type f -name "*.mp3" | wc -l )"
typeset -i i
i=0
function getLastNum()
{
for file in $FILES
do
echo $file
# nstring=${file##*/} #gets just the file name
lstring=${file##*"("} #chops off Lside to the ( leaving num )
rstring=${lstring%%")"*} #chops off Rside to the ) leaving the num
arr[$i]=$rstring
(( i++ ))
done
for (( a=0; a < $countnum ; a++ )) ; do
if (( arr[a+1] < arr[a] )); then
store=arr[$a]
arr[$a]=arr[$a+1]
arr[$a+1]=$store
BigNum=$store
fi
done
return $BigNum
}
getLastNum
as such
Code:
userx@voider~:>> nstring="noere(1)"
userx@voider~:>> lstring=${nstring##*"("}
userx@voider~:>> echo $lstring
1)
userx@voider~:>> rstring=${lstring%%")"*}
userx@voider~:>> echo $rstring
1
now you will know where you left off at.
Code:
lastNum="$(( $BigNum+1 ))"
gives you the next number to use.
another snippet of my script to give you an idea on how to get the just the file name then add the number to the next file. using
basename and
dirname too can be used instead if you like.
Code:
c=$FILENAME
xpath=${c%/*}
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
pref=${xpref}
ext=${xfext}
newFile="$pref-(Lost-Song-Number-("$lastNum")).mp3"
Manipulating Strings
Bash String Manipulation Examples – Length, Substring, Find and Replace