Created
July 2, 2020 16:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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