32

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)
Zanna
  • 69,223
  • 56
  • 216
  • 327
Sumeet Deshmukh
  • 8,628
  • 11
  • 55
  • 93
  • For separate checksum files for each subfolder, see also [this answer](https://stackoverflow.com/a/71286224/111036) – mivk Feb 27 '22 at 17:18

2 Answers2

58

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)

Zanna
  • 69,223
  • 56
  • 216
  • 327
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
Yaron
  • 12,828
  • 7
  • 42
  • 55