You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
64 lines
1.5 KiB
#!/bin/bash
|
|
|
|
# Just a normal copy program, BUT it never overwrites a file in the destination.
|
|
# if the filename is already taken, the new file will get an incrementing number as postfix added just in fron of the dot before the extension.
|
|
# no extension, its added the end.
|
|
# example: filename.wav already exists, new file will get the name filename_1.wav.
|
|
#
|
|
# usage cp_keep.sh [src file] [dest folder] [prefix newfile]
|
|
|
|
export IFS=$'\n'
|
|
src=$1
|
|
dst=$2
|
|
prefix=$3
|
|
|
|
if [ -z $1 ]; then {
|
|
echo "must specify source file"
|
|
exit
|
|
}; fi
|
|
|
|
if [ -z $2 ]; then {
|
|
echo "must specify destination directory"
|
|
exit
|
|
}; fi
|
|
|
|
if [ ! -d $2 ]; then {
|
|
echo "destination must be a directory"
|
|
exit
|
|
}; fi
|
|
|
|
if [ -d $1 ]; then {
|
|
echo "$1 is directory. .. quitting."
|
|
exit
|
|
}; fi
|
|
|
|
src_dir=$(dirname $src)
|
|
src_file=$(basename $src)
|
|
|
|
if [ "$dst" -ef "$src_dir" ]; then {
|
|
echo "source and destiation are same directory... quitting"
|
|
exit
|
|
}; fi
|
|
|
|
export fname_new=$prefix$src_file
|
|
|
|
count=1
|
|
while [ -e $dst/$fname_new ]; do { #destination filename already taken?
|
|
#add suffix before any dot, if no dot just append
|
|
if [[ "$fname_new" =~ \. ]]; then {
|
|
fname_new=$(echo $fname_new | sed -e "s/\./_$count\./g")
|
|
}; else {
|
|
fname_new=$fname_new"_$count"
|
|
}; fi
|
|
# echo "filename already taken, suffix added: $dst/$fname_new"
|
|
count=$(($count + 1))
|
|
}; done
|
|
|
|
if [ "$count" -gt 1 ]; then {
|
|
echo "$dst/$src_file already existed, new name: $fname_new"
|
|
}; fi
|
|
|
|
export cmd="cp -p \"$src_dir/$src_file\" \"$dst/$fname_new\""
|
|
|
|
#echo $cmd;
|
|
eval $cmd
|
|
|