I would like to rename some files to their contents' MD5 sum; for example, if file
foo is empty, it should be renamed to
Does it have to be script or can I use something like the
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Glenn's answer is good; here's a refinement for multiple files:
md5sum file1 file2 file3 | # or *.txt, or whatever while read -r sum filename; do mv -v "$filename" "$sum" done
If you're generating files with
find or similar, you can replace the
md5sum invocation with something like
find . <options> -print0 | xargs -0 md5sum (with the output also piped into the shell loop).
This is taking the output of
md5sum, which consists of multiple lines with a sum and then the file it corresponds to, and piping it into a shell loop which reads each line and issues a
mv command that renames the file from the original name to the sum. Any files with identical sums will be overwritten; however, barring unusual circumstances (like if you're playing around with md5 hash collisions), that will mean they had the same contents, so you don't lose any data anyway. If you need to introduce other operations on each file, you can put them in the loop, referring to the variables
$sum, which contain the original filename and the MD5 sum respectively.
autoload zmv # best in ~/.zshrc zmv '**/*(#qD.)' '$(md5sum<$f)'
Would rename all the regular files, recursively.
(note that it will refuse to rename any file if two files in the same directory have the same checksum, add
-f to override that).
zsh's recursive glob with glob qualifiers (in their explicit form as
D to include hidden files,
. to only select regular files.