Rename all files in a directory to the md5 hash

for i in *; do sum=$(md5sum $i); mv -- "$i" "${sum%% *}"; done

2
By: malsploit
2019-10-17 08:46:22

What Others Think

Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like that you pay to write my dissertation. Thanks a million and please keep up the effective work
hillarydavidson · 8 weeks and 4 days ago
I guess it would be good if we add some error handling. Two or more files could hash to the same checksum - the so called collision - and vanish without a trace. I did a little experiment - touched 10 empty files (I am using zsh, my favorite shell)... for ((i=0; i<10; ++i)) for> touch file_$i Next I ran the above command: for i in *; do sum=$(md5sum $i); mv -- "$i" "${sum%% *}"; done ls d41d8cd98f00b204e9800998ecf8427e This just gave me one file... Some kind of warning message - or skipping a colliding file would be good.
atul_s_khot · 5 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: