Skip to content

Instantly share code, notes, and snippets.

@macintacos
Created March 7, 2017 17:27
Show Gist options
  • Save macintacos/04d8190cf907d1dd843cd82b6d37471d to your computer and use it in GitHub Desktop.
Save macintacos/04d8190cf907d1dd843cd82b6d37471d to your computer and use it in GitHub Desktop.
Crazy dirty script to prepend all filenames in a directory with ISO8601-formatted dates
for f in *;
do
filename=$(basename "$f")
filenamespacereplace=${filename// /-}
filecreationdate=$(GetFileInfo -d "$f")
truncateddate=${filecreationdate::10}
dateslashreplace=${truncateddate////-}
truncatedyear=${dateslashreplace:6:9}
truncatedday=${dateslashreplace:3:2}
truncatedmonth=${dateslashreplace::2}
# build the correct date format
correcteddate=$truncatedyear
correcteddate+="-"
correcteddate+=$truncatedmonth
correcteddate+="-"
correcteddate+=$truncatedday
echo File name: $filename;
echo Creation date: $correcteddate
newfilename=$correcteddate
newfilename+="-"
newfilename+=$filenamespacereplace
echo New name: $newfilename
mv ./$filename $newfilename
echo Successfully converted
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment