Skip to content

Instantly share code, notes, and snippets.

@btcrooks
Created January 1, 2016 01:11
Show Gist options
  • Save btcrooks/55caa357276667992c87 to your computer and use it in GitHub Desktop.
Save btcrooks/55caa357276667992c87 to your computer and use it in GitHub Desktop.
Set a file's creation date as it's filename
#!/usr/bin/env bash
#
# Retrievs a file's modified date and
# set's it as it's fielname.
#
declare -a files="${1}"
## Check if file has been specified
if [[ -z ${files} ]]; then
echo "No file(s) specified"
exit 1
fi
echo $files
for i in $files; do
## Get file info
declare file_extension="${i##*.}"
## it's a hack… i know
declare file_date=$(stat -x ${i} | grep -e "Modify" | cut -f2-5 -d " ")
declare new_file_name="${file_date}.${file_extension}"
new_file_name=${new_file_name// /_}
new_file_name=${new_file_name//:/-}
echo "File: ${i}"
echo "Type: $file_extension"
echo "Date: ${file_date}"
echo "New Filename: ${new_file_name}"
echo ""
echo "Changing file name…"
mv "${i}" "${new_file_name}"
if [ "$?" != "0" ]; then
exit 1
fi
done
echo ""
echo "All done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment