8

So I'm trying to check dozens of files using a shell script. The file check happens at different times.

Is there a way to do this?

md5sum -c 24f4ce42e0bc39ddf7b7e879a File.name

or even better sha512sum

sha512sum -c 24f4ce42e0bc39ddf7b7e879a File.name

Right now I have to do this:

md5sum -c file.md5sums File.name

Or better yet I could have all the md5sums in a single file and check them like this:

md5sum -c `sed 1p file.md5sums` File.name
md5sum -c `sed 2p file.md5sums` File.name
md5sum -c `sed 3p file.md5sums` File.name
md5sum -c `sed 4p file.md5sums` File.name

It just seems silly to have dozens of files with single entries in them.

4 Answers 4

4

I was able to do a sha256sum on multiple file, write the output to a text file and run a sha256sum -c sumfile and it seemed to work for me.

$ sha256sum $(find /etc/ -maxdepth 1 -type f) > test.txt
$ sha256sum -c test.txt
...
/etc/statetab: OK
/etc/sysctl.conf: OK
/etc/system-release: OK
/etc/system-release-cpe: OK
/etc/termcap: OK
/etc/updatedb.conf: OK
/etc/vconsole.conf: OK
/etc/vimrc: OK
/etc/virc: OK
/etc/yum.conf: OK
...
$ sha256sum --version
sha256sum (GNU coreutils) 8.22
1
  • The problem is "sha256sum -c test.txt" checks all the files at once. I'm running canned data through pipeline and checking files as they are formed. This is a quality control / integrity check. If one part of the pipeline produced bad test results then I abort. The other two posters supplied working solutions. "md5sum -c -" as well as "if [ "$(md5sum < File.name)" = "24f4ce42e0bc39ddf7b7e879a -" ]" both work.
    – Jon17
    Commented Jun 17, 2016 at 16:13
4

The md5sum utility, as all the other similar utilities in the GNU coreutils collection, is able to take a file of checksums and verify these against the corresponding files in the filesystem.

Let's say I've generated the checksum file like this:

$ md5sum /etc/* >sums
$ cat sums
e55afe6e88abb09f0bee39549f1dfbbd  /etc/afpovertcp.cfg
279f7ab7d2609163e5034738b169238b  /etc/aliases
5c1ba75b6d9d8cf921ec83e2a54c9bb5  /etc/asl.conf
[...]
d41d8cd98f00b204e9800998ecf8427e  /etc/xtab
32d37eb59a7c3735635db329adad86d7  /etc/zprofile
4efb8dbeb8f46ca3879666b313a2607f  /etc/zshrc

I can then verify all those checksums in one go, like this:

$ md5sum -c sums
/etc/afpovertcp.cfg: OK
/etc/aliases: OK
/etc/asl.conf: OK
[...]
/etc/xtab: OK
/etc/zprofile: OK
/etc/zshrc: OK

If I misunderstood you and you just want to check one particular file out of several in your sums file, then I'd do like this:

$ fgrep "/etc/xtab" sums | md5sum -c -
2
  • The last line works! Thank you! "fgrep "/etc/xtab" sums | md5sum -c -"
    – Jon17
    Commented Jun 17, 2016 at 16:09
  • @Jon17 Ah, so that's what you wanted...
    – Kusalananda
    Commented Jun 17, 2016 at 16:10
2

If you're doing this in a script then you can just do a simple comparison check e.g.

if [ "$(md5sum < File.name)" = "24f4ce42e0bc39ddf7b7e879a  -" ]
then
  echo Pass
else
  echo Fail
fi

Note the extra spaces and - needed to match the output from md5sum.

You can make this a one-liner if it looks cleaner

[[ "$(md5sum < File.name)" = "24f4ce42e0bc39ddf7b7e879a  -" ]] && echo Pass || echo Fail
0
0

For single files you can do this (in bash) to pass a string as a file

md5sum -c <(echo "24f4ce42e0bc39ddf7b7e879a File.name") File.name

You must log in to answer this question.

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