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.
67 lines
1.5 KiB
67 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';
|
|
export src=$1;
|
|
export dst=$2;
|
|
export 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
|
|
|
|
export src_dir=$(dirname $src);
|
|
export 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 \"$src_dir/$src_file\" \"$dst/$fname_new\"";
|
|
|
|
#echo $cmd;
|
|
eval $cmd;
|
|
|