-
Notifications
You must be signed in to change notification settings - Fork 2
/
mv-addmoddate
executable file
·76 lines (65 loc) · 2.16 KB
/
mv-addmoddate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
set -e
# convert a pdf into grayscale
function func-mv-addmoddate() {
# no! local args=($*) -- does not handle quotes.
# no! local narg=${#args[@]}
local narg=$#
if [ $narg -lt 2 ]
then
echo "USAGE: addname-moddate [<opt>] <file1> ... <fileN> <outdir>'"
echo "Renames each file by adding modification date to each filename (renames filename.ext to filename_<date>.<ext>)"
echo "and moves to the target directory <outdir>."
echo ""
echo "<opt> (Optional) options:"
echo " --dryrun Do not move any file, just print mv commands"
echo " --names-only Print the new names only line by line, without any path prefixes. Ignore outdir, do not alter any file."
echo ""
echo "Reports errors for already existing target filenames and continues with the remaining ones."
exit 1
fi
outdir="${@: -1}"
if [ -d "$outdir" ]; then
pass=1
else
echo "Target directory path is invalid: $outdir"
exit 2
fi
mode="normal"
i0=1
if [ "$1" = "--dryrun" ]; then
mode='dryrun'
i0=2
elif [ "$1" = "--names-only" ]; then
mode='names-only'
i0=2
fi
for (( i = i0 ; i <= ($narg-1); i++ ))
do
# separate input name and extension
local f="${!i}"
f0=$(parse-path -fname-noext "$f")
ext=$(parse-path -ext "$f")
# partly from https://unix.stackexchange.com/questions/353083/add-modification-time-to-the-filename
outname="${f0}_$(date -r "$f" +'%Y-%m-%d_%H-%M')${ext}"
out="${outdir}/${outname}"
if [ -e "$out" ]; then
print "ERROR: Already exists: $out$"
else
if [ $mode = 'normal' ]; then
echo "$f -> $out"
mv -n "$f" "$out"
elif [ $mode = 'dryrun' ]; then
echo mv -n "$f" "$out"
elif [ $mode = 'names-only' ]; then
echo "$outname"
else
echo "UNKNOWN MODE (BUG)"
exit 9
fi
fi
done
exit 0
}
# ok with multiple quoted arguments
func-mv-addmoddate "$@"