Created
December 24, 2018 12:57
-
-
Save caramelchocolate/c2d1e203702acc4647c6fb9956927597 to your computer and use it in GitHub Desktop.
Rename a file to sha512 hash of any string.
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
# This command renames files. | |
find ./ ! -name '*.DS_Store' -type f -print0 | xargs -0 -L1 sh rename_sha512.sh |
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/sh | |
# This program renames a file. | |
# The file name is sha512 hash of any string. | |
file_name=${1:-""} | |
if [ ! -f "${file_name}" ]; then | |
echo "No such file." >&2 | |
exit 1 | |
fi | |
base_dir=$(dirname "${file_name}") | |
# openssl sha512 "${file_name}" => SHA512(.//file_name)= 6790faba734484b... | |
hash=$(shasum -a 512 "${file_name}" | awk -F' ' '{print $1}') | |
mv -iv "${file_name}" "${base_dir}/${hash}.${file_name##*.}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment