diff --git a/cp-date b/cp-date index c7079d4..3f9882d 100755 --- a/cp-date +++ b/cp-date @@ -1,12 +1,18 @@ #!/usr/bin/env bash usage() { echo -e "Make copy of file with last change date at end.\nUSAGE: $0 myfile # makes myfile.YYYY-MM-DD"; } [[ $# -eq 0 || $* =~ ^-+h ]] && usage && exit 1 + +# move if first arg is --mv +cp=cp +[[ "$1" == '--mv' ]] && cp=mv && shift 1 + while [ $# -gt 0 ]; do -infile="${1:?file to copy}"; shift -[ ! -r "$infile" ] && warn "Bad file: '$infile'" && continue + infile="${1:?file to copy}"; shift + [ ! -r "$infile" ] && warn "Bad file: '$infile'" && continue + [[ ${infile} =~ /$ ]] && infile=${infile::-1} -newname=$infile.$(find "$infile" -printf '%CF') -[ -r "$newname" ] && warn "$newname aleady exists!. remove and rerun to replace" && continue + newname=$infile.$(find "$infile" -maxdepth 0 -printf '%CF') + [ -r "$newname" ] && warn "$newname aleady exists!. remove and rerun to replace" && continue -dryrun cp "$infile" "$newname" + dryrun $cp "$infile" "$newname" done