-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcp-date
More file actions
executable file
·23 lines (18 loc) · 843 Bytes
/
cp-date
File metadata and controls
executable file
·23 lines (18 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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} =~ /$ ]] && infile=${infile::-1}
newname=$infile.$(find "$infile" -maxdepth 0 -printf '%CF')
[ -r "$newname" ] && warn "$newname aleady exists!. remove and rerun to replace" && continue
# handle directory copy (recursive) [added 20251024]
args=()
[[ $cp == "cp" && -d "$infile" ]] &&
warn "# cp on dir '$infile'; running 'cp -r'. hope that's what you want" && args=(-r)
dryrun $cp "${args[@]}" "$infile" "$newname"
done