: ########################################################################## # Title : replace - replace older with newer files # Author : Heiner Steven # Date : 1994-01-25 # Requires : # Category : File Utilities # SCCS-Id. : @(#) replace 2.1 14/05/01 ########################################################################## # Description # ########################################################################## PN=`basename "$0"` # Program mname VER='2.1' usage () { echo >&2 "$PN - replace older files with newer ones, $VER (stv '95) usage: $PN [-av] file [file ...] targetdir -a: (add) copy files not contained in targetdir -F: (force) ignore errors (e.g. non-existing files). Otherwise $PN will abort execution at first error. -v: (verbose) print diagnostics Copied files get the modification timestamp of the source files, not the current time." exit 1 } msg () { [ "$Verbose" = no ] && return 0 for i do echo "$PN: $i" >&2 done } fatal () { Verbose=true; msg "$@"; exit 1; } # newer - Test if file1 is newer (or same age) as file2 newer () { [ $# -ne 2 ] && return 1 # Uncomment the following code block if you have a newer # shell like ksh(1) or bash(1) supporting the -nt (newer # time) test operator #if [ "$1" -nt "$2" ] #then return 0 #else return 1 #fi # If both files have the same date, some systems return the # first file name given to "ls"; some return the second Newest=`ls -t "$2" "$1" | head -1` [ -z "$Newest" ] && return 2 # error if [ "$Newest" = "$1" ] then return 0 else return 1 fi } Verbose=false # parameter -v Add=false # copy new files (true/false) ForceRun=false while [ $# -gt 1 ] do case "$1" in -a) Add=true ;; -F) ForceRun=true ;; -v) Verbose=true ;; -*) usage ;; *) break ;; esac shift done if [ $# -lt 2 ] then usage else # Get last command line argument, the destination directory. TargetDir= for TargetDir do : # We are just interested in the very last positional argument done [ -d "$TargetDir" ] || fatal "ERROR: directory not found: $TargetDir" fi error=0 copycount=0 while [ $# -gt 1 ] # Omit last argument (target directory) do Source=$1; shift if [ -f "$Source" ] then Name=`basename "$Source"` Copy=false if [ -f "$TargetDir/$Name" ] then # File exists in target directory newer "$Source" "$TargetDir/$Name" && Copy=true else if [ "$Add" = "true" ] then Copy=true else msg "not in target directory: '$Name'; ignored" fi fi if [ "$Copy" = "true" ] then msg " replacing $TargetDir/$Name" # Copy file keeping the source file's time stamp. # Note that superfluous copying can happen in the # following case: # o The file sytem keeps the modification time # stamp in a resolution higher than 1 second # (e.g. btrfs). # o The "cp -p" implementation does truncate the # file modification timestamp of the destination # file to complete seconds (e.g. GNU cp(1) 8.21) if cp -p "$Source" "$TargetDir/$Name" then copycount=`expr "$copycount" + 1` else error=1 # cp(1) already printed an error message fi fi else error=1 if [ "$ForceRun" = "true" ] then msg "WARNING: file '$Source' not found - ignored" else msg "ERROR: file '$Source' not found" break fi fi if [ "$error" != "0" ] && [ "$ForceRun" != "true" ] then break # Error message was already printed fi done msg "replaced $copycount file(s)" exit $error