0

I've got .txt list with files and corresponding MD5 hash:

ab16d9ab5682bb0038c901ad8fc424c0 *file1.doc
28bb8e092301340f19585201166e5b86 *file2.doc
5f53d5e9512a717049185fcf617b8a37 *file3.doc
3c28de2b988ce82b04803671540427e5 *file4.doc
75079fb178347e3f1b3b886589ca6471 *file5.doc

When .txt file is in directory:

  1. How to rename files to their hashes?
  2. How to rename hashes to their filenames?
5
  • Are your files really named *file1.doc? Is the * part of the file name? – terdon Oct 5 '20 at 12:23
  • @terdon * is a flag that md5sum uses to indicate a binary file I think – steeldriver Oct 5 '20 at 12:34
  • @steeldriver ah, OK. In that case, we'd need to remove it. Thanks – terdon Oct 5 '20 at 12:56
  • For clarification, would you mind editing the post to add the desired output for the input example you showed? – AdminBee Oct 5 '20 at 14:17
  • 1
    Thanks @αғsнιη. Working now after remove 'echo'. Thanks again! – jack67 Oct 6 '20 at 11:50
1

How to rename files to their hashes?

while IFS=' ' read <&3 -r hash fileName; do
    echo mv -v -- "${fileName#\*}" "${hash}";
done 3< infile.txt

How to rename hashes to their filenames?

while IFS=' ' read <&3 -r hash fileName; do
    echo mv -v -- "${hash}" "${fileName#\*}";
done 3< infile.txt

note: remove echo when you were happy with dry-run. -v is a non-standard extension supported by some mv implementations to tell them to be more verbose. If your mv doesn't support it, just remove it.

We open infile.txt on file descriptor 3 instead of stdin (0) so that it doesn't interfere with mv user prompting, if any.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.