0

I tried with

find /bin -type -f -exec md5sum {} + >>sum.md5 

The /bin directory contains a variety of files types, but the above returns the checksums for only regular files. Is there any way to find all files in and under /bin, run md5sum on them, and verify the checksums?

Here's the detail of what I want to achieve. We take the backup of the /bin and its subdirectories and md5sum all the files into an old.md5 file, tar it and upload to cloud. For example I had a crash in my system then I downloaded the /bin directory from the cloud and restore in, and again run the md5sum command to generate a new.md5 with hashes of all files. Now I want to compare the new.md5 file and old.md5 file to make sure no changes have takken to the file during download or upload. The script should verify the hashes.

I need a script for the entire thing.

8
  • 1
    Welcome on U&L! Your command does indeed also descend into subdirectories. It is not clear from your question if you are fine with the results from this first step. Are you just asking how to perform a check based on the sum.md5 file you generated?
    – fra-san
    Commented Sep 11, 2020 at 8:15
  • I don't understand what you mean by "all files" that your command doesn't already do. What's the problem? Are you hitting a permissions issue? You don't say Commented Sep 11, 2020 at 8:51
  • @roaima "all files" in the sense all type of files like regular file character file block file etc.
    – Dilip Rao
    Commented Sep 11, 2020 at 11:38
  • character files and block files are actually devices. There shouldn't be any of those under /bin Commented Sep 11, 2020 at 11:39
  • @roaima I am looking for a script which checks the hash of all files in /bin and its sub directories and verify the hash is correct and no changes have made to file. I tried with " tree /bin | md5sum -c output.md5 " note: output.md5 is the hash file. It is not able to read the files in sub directories and i am not able to find the logic to verify and confirm if the hash has changed or not in bash scripting
    – Dilip Rao
    Commented Sep 11, 2020 at 11:40

2 Answers 2

2

To find all non-directory files using find, use a test that weeds out directories:

find -H /bin ! -type d -exec md5sum {} + >sum.md5

This will run md5sum in batches on as many files as possible, including symbolic links, while avoiding giving the utility directories (which would make it issue a warning).

The -H option to find makes it resolve any symbolic links given on the command line, i.e. /bin (if that's a symbolic link).

To verify the calculated checksums, use md5sum -c sum.md5.

1

Your command already descends into subdirectories (unless /bin is a link, see below) but it will skip links and other non-regular files because you are explicitly telling find to only return regular files. That's what -type f means. From man find:

   -type c
          File is of type c:

          f      regular file

To find everything, just remove the -type:

find /bin/ -exec md5sum {} + >>sum.md5

Note the / at the end of /bin/: that is needed in case your /bin is a symlink (as it often is a symlink to /usr/bin on modern Linux systems). Without it, the command will only return the symlink itself (/bin) and will not descend into subdirectories.

If you later want to verify these checksums, just run:

md5sum -c sum.md5 
8
  • @terdon Thanks for the input i am trying to restore the /bin and contents in a remote server only restore if the md5sum is correct do you have any script to verify the md5sum and restore only if it is ok .
    – Dilip Rao
    Commented Sep 11, 2020 at 8:38
  • 1
    @DilipRao that is a different question. If you run md5sum -c sum.md5, that will report whether the checksum matched, so you can use that output to restore. If you need help with that, please ask a new question, giving an example sum.md5 command and the script you have so far.
    – terdon
    Commented Sep 11, 2020 at 8:40
  • 1
    @DilipRao You mean you want to restore the files that have failing checksums? It sounds like you'd be better off just running rsync from a known good system, or make the local system's package manager reinstall the affected packages. This is, as terdon points out, quite a different question though.
    – Kusalananda
    Commented Sep 11, 2020 at 8:44
  • It sounds like a reimplementation of tripwire Commented Sep 11, 2020 at 11:53
  • @terdon find /etc ! -type d -exec md5sum {} + >>iutput.md5 give output as md5sum it is a directory " Any other way to md5sum for all files in all sub directories.
    – Dilip Rao
    Commented Sep 11, 2020 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .