How can I recursively list Md5sum of all the files in a directory and its subdirectories?
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
Related videos on Youtube
![Sumeet Deshmukh](https://i.stack.imgur.com/QH4wY.jpg?s=256&g=1)
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, 2022Comments
-
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 over 2 yearsFor separate checksum files for each subfolder, see also this answer
- Integrate it within
-
Anwar about 7 yearsthis also creates md5sum of the
md5sum.txt
file itself. would be nice it wasn't -
Anwar about 7 yearsDoesn't work if there is a directory. Says
md5sum: /path_to_dir: Is a directory
-
Zanna about 7 years@Anwar see edit for slightly cumbersome exclusion ^_^
-
Anwar about 7 yearsAh! seen it. I better one line in a huge md5sum.txt then extra 5 lines of code. Anyway, thank
-
Yaron about 7 years@Anwar running
md5sum /path_to_dir/*
will executemd5sum
on every file in the directory -
Anwar about 7 yearsWhat about the other files inside subdirectories?
-
Yaron about 7 years@Anwar - it won't work on sub-directories (which was defined as nice to have in the question)
-
Anwar about 7 yearsbut I see this added to the edition, this is sort of important
-
user1985553 over 6 yearscan always do: "> ../md5sum.txt" to ensure that the file doesn't get included in the result
-
SmallChess about 6 yearsThis doesn't answer the question at all.
-
Roland Pihlakas about 6 yearsFor binary files don't forget to add -b after the md5sum command.
-
george over 3 yearsthis is all wrong.