How can I recursively list Md5sum of all the files in a directory and its subdirectories?

63,660

Solution 1

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)

Solution 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
Share:
63,660

Related videos on Youtube

Sumeet Deshmukh
Author by

Sumeet Deshmukh

Bonjour, I was a frustrated Windows user now converted completely to Ubuntu and learning the power of Linux, I'm not too good at code But I'm a curious person and I generally generate good questions from that curiousness. And the only thing that I know in French is "Bonjour" Few things you should know about me I Prefer Gnome with Adapta over Everything else. I'll always prefer GUI over CLI if that option is available. I generally use my computer for watching movies, listening to music and surfing the web. I'm planning to add more points in this bullet list soon.

Updated on September 18, 2022

Comments

  • Sumeet Deshmukh
    Sumeet Deshmukh almost 2 years

    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)
    • mivk
      mivk over 2 years
      For separate checksum files for each subfolder, see also this answer
  • Anwar
    Anwar about 7 years
    this also creates md5sum of the md5sum.txt file itself. would be nice it wasn't
  • Anwar
    Anwar about 7 years
    Doesn't work if there is a directory. Says md5sum: /path_to_dir: Is a directory
  • Zanna
    Zanna about 7 years
    @Anwar see edit for slightly cumbersome exclusion ^_^
  • Anwar
    Anwar about 7 years
    Ah! seen it. I better one line in a huge md5sum.txt then extra 5 lines of code. Anyway, thank
  • Yaron
    Yaron about 7 years
    @Anwar running md5sum /path_to_dir/* will execute md5sum on every file in the directory
  • Anwar
    Anwar about 7 years
    What about the other files inside subdirectories?
  • Yaron
    Yaron about 7 years
    @Anwar - it won't work on sub-directories (which was defined as nice to have in the question)
  • Anwar
    Anwar about 7 years
    but I see this added to the edition, this is sort of important
  • user1985553
    user1985553 over 6 years
    can always do: "> ../md5sum.txt" to ensure that the file doesn't get included in the result
  • SmallChess
    SmallChess about 6 years
    This doesn't answer the question at all.
  • Roland Pihlakas
    Roland Pihlakas about 6 years
    For binary files don't forget to add -b after the md5sum command.
  • george
    george over 3 years
    this is all wrong.