Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I ask for help regarding some scripting.

I have a directory "J:\To_Sort" which contains another 1164 directories. Inside the "To_Sort" directory I also have 37 *.md5 files. These MD55 files contain the checksum and the original paths to every single file & directory. They are named:

  • selected-dir-01.md5
  • selected-dir-02.md5
  • etc

I would like to restore the directory structure based on the .md5 files. So I'd have something like:

  • J:\To_Sort\Selected-Dir-01\all the directories & files that were originally here
  • J:\To_Sort\Selected-Dir-02\all the directories & files that were originally here
  • J:\To_Sort\Selected-Dir-03\all the directories & files that were originally here

Here is the content of an md5 file:

D754CEE4296B03062E53EA0FF7859F53 *photos\Foley_At_141_-_14-04-2009\small-psd\DSC_7975 copy.psd

The first section is a checksum, followed by a space. Then the asterisk denotes the start of the directory structure and file path. A CR denotes a new line in the MD5 file. MD5 files were created by FastSum command line edition

I found something here that could lead to a solution. (I'm not too familiar with Ruby, but I'm now dissecting the code.)

I started thinking about solutions and I think I'm overcomplicating it. (Writing a macro in UltraEdit to remove the CRC values from .md5s, replace asterisk with destination directory path, then write a batch file... I'm sure that there is a better solution.)

I have OS X Snow Leo (with Xcode) & Windows XP Home.

EDIT: As codelahoma quite rightly pointed out, I forgot to mention that the file names and the folder structure are intact. (They are the same as they are listed in the .MD5 files)

So This was the original structure (which I would like to regain):

J:\To_Sort\Selected-Dir-01\photos\Foley_At_141_-_14-04-2009\small-psd\DSC_7975 copy.psd

This is the current structure:

J:\To_Sort\photos\Foley_At_141_-_14-04-2009\small-psd\DSC_7975 copy.psd

Allowed characters in filename and extension: as per ISO9660/Joliet (Mode 2) standard.

I am eternally grateful for a solution!

share|improve this question
You haven't described where the files are now, and how they are named. Getting the directory information out of the md5 files should be pretty straightforward, but it's only step one. – codelahoma Dec 28 '13 at 21:18
A "hashtag" is something completely different. – Daniel Beck Dec 29 '13 at 1:01
Indeed! Post edited to reflect changes. – b-varga Dec 29 '13 at 1:14
awk 'BEGIN { FS=" \\*|\\\\" }
           { print gensub(".md5","", "1", FILENAME) "\\" $2 }' *.md5 |
  sort -u |
  while read -r line; do echo mv "${line#*\\}" "${line%\\*}"; done

Output from the example data you gave:

mv photos selected-dir-01

Remove the "echo" from before "mv" if the output looks good.

edit: Oh, and make the dirs for stuff to be moved into, of course:

for f in *.md5;do mkdir "${f%.md5}";done
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .