hybra
hybra

Reputation: 11

Get list of file names and their md5 hashes into text file

I wrote this command to find all .txt file names and sort/deduplicate them then saving into a text file.

find . -type f -name '*.txt' -exec basename {} \; | sort | uniq - txtsort.txt

this way I get the file filled with the unique filenames

...
Empty.txt
...

How do I concatenate the files md5 hashes to their file names?

Like:

...
d41d8cd98f00b204e9800998ecf8427e_Empty.txt
...

Upvotes: 0

Views: 1434

Answers (2)

hybra
hybra

Reputation: 11

I found that using md5 -r answers my question (I don't really need the underscore in the name, it was just for aestetical purposes), so:

find . -type f -name '*.txt' -exec md5 -r {} \; | sort | uniq - txtsort.txt

will do the trick.

Upvotes: 0

Mihir Luthra
Mihir Luthra

Reputation: 6779

find . -type f -name '*.txt' -exec bash -c "a=\$(basename {} | md5) ; mv {} \$(dirname {})/\$a\_\$(basename {}) " \;

This should do the trick.

find . -type f -name '*.txt' : Goes through all .txt files.

a=\$(basename {} | md5) : Stores md5 hash of current file's basename.

mv {} \$(dirname {})/\$a\_\$(basename {}) : Renames the current file by adding $a i.e. the has separates by a _.

Upvotes: 1

Related Questions