I recovered some files from my HDD, but there names were changed. Also, I found md5 files of the recovered files.

How can I use the md5 file to restore files names?


First rename (or link) each recovered file to a name that contains its MD5.

Then loop over the file (perhaps using while read md5 name) and rename (or link) the md5name back to the original file name.

assuming the file is of the format produced by md5sum something like this will work.

for x in * 
  mv "$x" `md5sum < "$x" | cut -d' ' -f1`

cat /elsewhere/md5file |
while read md5 name 
  mv "$md5" "$name"

be sure not to rename the MD5 list in the first step. I have assumend it's in the fictional location /elsewhere/ you could put in your home or in /tmp/ etc.


Try in one step:

md5sum * | awk 'NR==FNR {FN[$1] = $2; next} {print "mv -v \"" $2 "\" \"" FN[$1] "\""}' md5file -

This creates a md5sum signature for every file in your working directory, pipes it into an awk script that reads the "old" filenames from the old md5 file into an array indexed by the signature, then reads the actual file names from stdin (-, the pipe) and prints the respective mv command to stdout. Pipe this into a shell (| sh) if happy with what is proposed.

  • File name has white spaces – user310967 Sep 15 '18 at 9:29
  • OK, add double quotes around both source and target file names. – RudiC Sep 15 '18 at 9:31
  • It didn't work , and its just prints the file its not rename them – user310967 Sep 15 '18 at 11:40
  • @C.ClaryChristion Read the last part of the answer again. – Kusalananda Sep 15 '18 at 12:38

Your Answer

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