#! /bin/bash # ## Find files in dir1 which are newer than files in dir2 # ## This shell script returns a list of files which are either # newer than their counterparts in dir2 or do not exist there. If # invoked with the switch -v the output lines are prefixed with "C" # for files which are existant but older in dir2 then in dir1 and "N" # for files non-existant in dir2. # If dir2 is not specified it defaults to the current directory. # ## GPL 2007 Manfred Waßmann http://www.berlinos.de/ ## # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2, # as published by the Free Software Foundation. # # new,v 1.2 2007-09-25 19:40:27 manolo Exp # usage () { local rc="${1:-0}" echo "usage: ${0##*/} [-vD] [--] DIR1 [DIR2]" exit $rc } newer () { local path1="$1" path2="$2" if [ -d "$path1" ]; then for file in "$path1"/*; do test -e "$file" || continue newer "${path1}/${file##*/}" "${path2}/${file##*/}" done elif [ -e "$path1" ]; then if [ "$VERBOSE" ]; then if [ ! -e "$path2" ]; then printf '%s %s\n' N "$path1" elif [ "$path1" -nt "$path2" ]; then printf '%s %s\n' C "$path1" fi elif [ ! -e "$path2" -o "$path1" -nt "$path2" ]; then echo "$path1" fi fi } unset VERBOSE while getopts :vD OPT; do case $OPT in h) usage ;; v) VERBOSE=-v ;; D) set -x ;; *) usage 2 esac done shift $[ OPTIND - 1 ] if [ $# -eq 1 ]; then newer "$1" . elif [ $# -ne 2 ]; then usage 2 else newer "${@%%/}" fi