23

I want to list (and save) Md5 check sum of all the files in a directory and save that list in a text file called md5sum.txt

it would be also nice if I could

  • Integrate it within tree command (which creates a tree structure of folders and files)
  • Make it work on Folders and Sub folders (this is sort of important)
0
42

You could use find (in the directory)

find -type f -exec md5sum '{}' \; > md5sum.txt

If you want to exclude the md5sum.txt file itself, you can do so:

find -type f \( -not -name "md5sum.txt" \) -exec md5sum '{}' \; > md5sum.txt

You can also use a loop: turn on recursive globbing

shopt -s globstar

Then, in the directory:

for i in **; do [[ -f "$i" ]] && md5sum "$i" >> md5sum.txt; done

You can exclude the file itself from this one as well:

for i in **; do 
  [[ -f "$i" ]] && 
  [[ md5sum.txt != "$i" ]] && 
  md5sum "$i" >> md5sum.txt
done

Neither of these produces a tree-like structure. But they do print the relative path to the file from the starting directory. If you want the absolute path, use find /path/to/directory ...

You might want to turn off globstar afterwards (shopt -u globstar)

5
  • 1
    this also creates md5sum of the md5sum.txt file itself. would be nice it wasn't – Anwar Apr 23 '17 at 6:51
  • 2
    @Anwar see edit for slightly cumbersome exclusion ^_^ – Zanna Apr 23 '17 at 6:55
  • 1
    Ah! seen it. I better one line in a huge md5sum.txt then extra 5 lines of code. Anyway, thank – Anwar Apr 23 '17 at 7:02
  • 3
    can always do: "> ../md5sum.txt" to ensure that the file doesn't get included in the result – user1985553 Feb 10 '18 at 20:25
  • For binary files don't forget to add -b after the md5sum command. – Roland Pihlakas May 13 '18 at 14:16
2

You can execute the following command:

md5sum /path/to/directory/* > /path_to_result/md5sum.txt

The output in the result file will be something like that:

46684e3891d990acde2e723ee3d4e94a  /var/log/alternatives.log
39cf1ebf93452ed5f8b240b35ae73f9f  /var/log/alternatives.log.1
aa6c09c411d1d0870bca5f401d589332  /var/log/alternatives.log.2.gz
7
  • 2
    Doesn't work if there is a directory. Says md5sum: /path_to_dir: Is a directory – Anwar Apr 23 '17 at 6:53
  • @Anwar running md5sum /path_to_dir/* will execute md5sum on every file in the directory – Yaron Apr 23 '17 at 7:23
  • What about the other files inside subdirectories? – Anwar Apr 23 '17 at 7:25
  • 1
    @Anwar - it won't work on sub-directories (which was defined as nice to have in the question) – Yaron Apr 23 '17 at 7:27
  • 1
    but I see this added to the edition, this is sort of important – Anwar Apr 23 '17 at 7:32

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