dotfiles/.local/bin/scripts/predate

19 lines
677 B
Plaintext
Raw Normal View History

2021-11-25 12:20:13 +00:00
#!/bin/sh -e
# Add the modification date in front of the filename
2023-11-04 18:08:39 +00:00
test $# -eq 0 && echo "predate [-s] [newname] <files...>" && exit 2
2021-11-25 12:20:13 +00:00
IFS='\n'
2022-01-11 12:23:56 +00:00
test "$1" = -s && short=true && shift
2023-11-04 18:08:39 +00:00
# Whether to change the filename suffix after the date
2023-08-07 15:45:59 +00:00
test ! -f "$1" && rename=true && name=$1 && shift || rename=false
2023-11-04 18:08:39 +00:00
for file
2021-11-25 12:20:13 +00:00
do mv --verbose --interactive "$file" \
2022-03-15 15:56:22 +00:00
"$(latest "$file" | head -2 | tail -1 |
2023-11-04 18:08:39 +00:00
cut -c$(test "$short" && echo "3,4,6,7,9,10" || echo "-10"))$(
2023-08-07 15:45:59 +00:00
if $rename
then echo "$(test "$name" && echo "_$name").${file##*.}"
else echo "_$file" | sed 's/^_2\?\([0-9]\{2,3\}\)-\([0-9]\{2\}\)\(-[0-9]\{2\}\)\?_\?/_/'
fi)"
done
2021-11-25 12:20:13 +00:00
# stat --format %y "$file" | cut -d' ' -f1