Bash get md5sum of all files in a folder

user577732 picture user577732 · Jun 20, 2012 · Viewed 10.9k times · Source

Hi I'm looking to see what file is changing in a directory i'd like to get the md5sum of every file and write it to a text file. Then after i know a file has changed i'd like to run it again so i can diff the output files to see what exactly changed. Here is what i've tried however it doesn't work as i need.

Also not only do i need to get the md5sum of every file in a folder including subdirectories i need it to not follow symlinks

#!/bin/bash
#

cd /sys/class
for i in $(find . -type f)
do
    ls -lt "$i" >> /home/george/Desktop/before.txt
done
echo "Finished!"

Thank you for any help

===Edit===

I put my actual paths in as i don't really see a need to hide them. Anyway running this returned only a few files (outputted file below) which are the files in the folders meaning it's not going into subdirectories and finding those files too. Btw sorry my bash is way rusty

--w------- 1 root root 4096 Jun 20 03:03 ./gpio/export
--w------- 1 root root 4096 Jun 20 03:03 ./gpio/unexport
-rw-r--r-- 1 root root 4096 Jun 20 03:03 ./firmware/timeout
-r--r--r-- 1 root root 4096 Jun 20 03:04 ./drm/version

===Edit2===

Not exactly sure why some of these files aren't being found for instance /sys/class/backlight/intel_backlight/brightness

And many others like that there are so many files that aren't being found for some reason

Answer

tripleee picture tripleee · Jun 20, 2012

The cd is unnecessary, and with type -f you are already in fact bypassing symlinks. So the loop is unnecessary, too:

find /path/to/directory -type f -exec md5sum {} + >before.txt

If your find is too old to support -exec {} + try with -exec {} \; instead.

For the md5sum comparison, you could try simply removing identical lines;

fgrep -vxf before.txt after.txt | less

This is assuming the list in before.txt will fit into fgrep; but if you are dealing with a few dozen thousand files tops, it can probably cope. This will not identify deleted files from before.txt, though.