Skip to content

Instantly share code, notes, and snippets.

@VaguelySerious
Created July 2, 2020 16:46
Show Gist options
  • Save VaguelySerious/dc335093c0fc62910792d31d36a03326 to your computer and use it in GitHub Desktop.
Save VaguelySerious/dc335093c0fc62910792d31d36a03326 to your computer and use it in GitHub Desktop.
Bash script to add a file's hash to it's own filename
#!/bin/bash
# Put this file in your /usr/local/bin directory and run "sudo chmod +x" on it.
# Use like so:
# addhash somefile.txt
# results in file being renamed to:
# somefile.9g2lap4.txt
for name in "$@"
do
sum=$(echo $name | md5sum | cut -c1-8)
withHash="mv $name ${name%.*}.$sum"
extension="${name##*.}"
if [ ! -z "$extension" ] && [ ! "$extension" == "$name" ]; then
withHash=$withHash.$extension
fi
$withHash
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment