#!/bin/sh - # # merge -- Non-destructive Move (Copy) files # # Usage: merge [-c] from to # merge [-c] files... dir # Options: -c copy instead of move the files # # This is equivelant to a `mv' operation except that files which would # overwrite an existing file (IE: file is destination directory has the # same name as a file being moved) will be renamed by appending a number # to it. The user is given a message when such renaming occurs, so # appropiate action can be taken. This is so that no file is destroyed # by a normal `mv' command especially when merging two directories of # filenames. # The `-c' flag will make this script use `cp' instead of `mv'. # # Author: Anthony Thyssen # # Check the type of shell that is running (For ULTRIX systems) [ "X$1" != 'X-sh5' -a -f /bin/sh5 ] && exec /bin/sh5 -$- "$0" -sh5 "$@" [ "X$1" = 'X-sh5' ] && shift sep='' # field seperator for internal filename list ( ':' when debuging ) attach='.' # string put between original name and appended number (dups) cmd="mv" doit() { # move/copy the file $1 to directory $2 as filename $3 n=1 # index for multiple items f="$3" # destination filename to try while [ -f "$2/$f" -o -d "$2/$f" ]; do n=`expr $n + 1` f="$3$attach$n" done [ $n -gt 1 ] && echo "file \"$3\" exists -> renamed \"$f\"" $cmd "$1" "$2/$f" } usage() { echo "Usage: merge [-c] from to" >&2 echo " merge [-c] files... dir" >&2 echo "Options: -c copy instead of move the files" >&2 echo "" >&2 exit 10 } case "$1" in -c) cmd="cp"; shift ;; esac case $# in 0|1) echo "merge: Too few arguments!" >&2 usage ;; 2) # handle direct filename to filename when arg 2 is not a directory if [ ! -d "$2" ]; then case $2 in /*) # Gad! -- a absolute filename given just move it doit "$1" "" "$2" ;; *) # ok just a relative filename move it to directory `.' doit "$1" "." "$2" ;; esac exit 0 fi ;; esac files="$1"; shift # first arg must be a filename to move/copy dir="$1"; shift # assume arg 2 is destination (sort it out below) while [ $# -gt 0 ]; do files="$files$sep$dir" # append the non-destination to file list dir="$1" # assume this is the destionation shift done # debugging -- check what is read in (change $sep above for testing) # echo "dir=$dir" # echo "files=$files" if [ ! -d "$dir" ]; then echo "merge: multiple files given, but destination is not a directory" >&2 usage fi oldIFS=$IFS; IFS=$sep; set -- $files; IFS=$oldIFS # For each file in list (no that we have a destination) move/copy it for i in "$@"; do doit "$i" "$dir" "`basename $i`" done