Skip to content

Instantly share code, notes, and snippets.

@caramelchocolate
Created December 24, 2018 12:57
Show Gist options
  • Save caramelchocolate/c2d1e203702acc4647c6fb9956927597 to your computer and use it in GitHub Desktop.
Save caramelchocolate/c2d1e203702acc4647c6fb9956927597 to your computer and use it in GitHub Desktop.
Rename a file to sha512 hash of any string.
# This command renames files.
find ./ ! -name '*.DS_Store' -type f -print0 | xargs -0 -L1 sh rename_sha512.sh
#!/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