I happen need to use the following shell script
find . -type f -exec sh -c '
mv "$0" "${0%/*}/$(printf "%s\n" "${0##*/}" | sha1sum | cut -d" " -f1)"
' {} \;
But I do not understand how does this script work? For instance, how to analyze
mv "$0" "${0%/*}/$(printf "%s\n" "${0##*/}" | sha1sum | cut -d" " -f1)"
' {} \;
piece by piece? Thanks.
${0##*/}takes the path$0and strips off any leading directory names, leaving only the file name. The printf command adds a newline to the end and then this file name is piped to…This computes the SHA-1 hash of the file name and then uses
cutto extract just the hash from sha1sum’s output.This is the opposite of
${0##*/}—this one gets the directories from$0and throws away the file name.So effectively, what ends up getting run is:
In English, it renames every file it finds to the SHA-1 hash of the original file name.
For what it’s worth, it could be simplified a bit and made more readable. I might write the mv command as: