Bash get md5sum of all files in a folder

11,361

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.

Share:
11,361
user577732
Author by

user577732

Updated on June 23, 2022

Comments

  • user577732
    user577732 about 2 years

    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

  • user577732
    user577732 about 12 years
    well the file list is definately pretty long which is why i'm definately thinking md5 isn't the best way to go but memory probably isn't a good way to go since the list is so long
  • Miquel
    Miquel about 12 years
    Ok, I've amended my solution to clarify the modification date, in case you can use that
  • user577732
    user577732 about 12 years
    well the idea is this my laptop has a backlit keyboard which the software is for windows i however run linux. The keyboard lights when i boot up however going to sleep and then waking it the keyboard stays off. So i'm looking to see what's changing in /sys/class (where i'm looking first) i assume some where a file is being changed telling the leds to turn off
  • Miquel
    Miquel about 12 years
    So, no security involved. Use ls -lt, and see what files have changed, that would be my advise. It doesn't even look like you'll have to script if for this particular scenario
  • user577732
    user577732 about 12 years
    yea none at all so i'll make an edit to my original post just to clarify for future users and myself and give it a run to see what happens
  • Miquel
    Miquel about 12 years
    Good luck! And for the record, your question's still a very good one and worthy of solving. It's just you can get away with less trouble if you want :) Speaking of that, I just saw @DavidSchwartz answer in the top question comments. Looks like that'd also help you!
  • user577732
    user577732 about 12 years
    Just added an edit to my original question it's not working for some reason
  • user577732
    user577732 about 12 years
    same thing as my edit still only outputted 4 things for some reason but like the solution very clean so far just not giving me everything for some reason
  • user577732
    user577732 about 12 years
    even did sudo before to make the command look like sudo find /sys/class -type f -exec ls -lt {} + >before.txt
  • user577732
    user577732 about 12 years
    it's not going into the subdirectories which is what i need as well that's what i just realized
  • user577732
    user577732 about 12 years
    it's not going into the subdirectories which is what i need