Skip to content

Instantly share code, notes, and snippets.

@maxlawton
Created December 22, 2017 17:20
Show Gist options
  • Save maxlawton/b5a54fd2cc6c6d7891daa329398ec5c3 to your computer and use it in GitHub Desktop.
Save maxlawton/b5a54fd2cc6c6d7891daa329398ec5c3 to your computer and use it in GitHub Desktop.
Date-move, prepend modtime to filenames
#!/bin/sh
# dmv: prefix each file with its modified date
# backup the 'field separator' characters and remove spaces from current IFS.
# this allows handling of files with spaces.
FMT="%Y%m%d-"
FML="%Y-%m-%d_"
FMH="%Y%m%d_%H%M-"
[ "-l" = "$1" ] && FMT="$FML" && shift;
[ "-t" = "$1" ] && FMT="$FMH" && shift;
[ "-h" = "$1" ] && FMT="$FMH" && shift;
[ "-s" = "$1" ] && shift;
SAVEIFS=$IFS
IFS=$'\t\n'
# this for loop iterates through all of the files that we gave the program
# it does one rename per file given
for file in $*
do
if [ -f $file -o -d $file ]
then
mv -i ${file} "$(/usr/bin/stat -t $FMT -f %Sm $file)${file}"
else
echo "${file} is not a file"
fi
done
# reset the IFS
IFS=$SAVEIFS
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment