How to check MD5 in multiple folders with one command ?

8 risposte [Ultimo contenuto]
teodorescup

I am a member!

Offline
Iscritto: 01/04/2011

Hello,
I need to check several directories contents and I want to do it with one command and I don't know how.

Any idea would be appreciated.

In each directory was run "md5sum * >md5.txt".

folder--
|subfolder1---md5.txt
---------------|--file1
---------------|--file2
|subfolder2---md5.txt
---------------|--file1
---------------|--file2

ivaylo
Offline
Iscritto: 07/26/2010

В 11:32 +0100 на 15.01.2012 (нд), teodorescup[@nospam] написа:
> Hello,

Hello.

> I need to check several directories contents and I want to do it with one
> command and I don't know how.
>
> Any idea would be appreciated.

Not exactly one command, but a one-liner, or a script.

$ find /tmp/some_dir/ -type d | while read dir; do md5sum ${dir}/* >
${dir}/md5sum.txt ; done # All in one line

$ ls -lR /tmp/some_dir/
/tmp/some_dir/:
total 8
-rw-r--r-- 1 username username 0 Jan 15 13:09 a
-rw-r--r-- 1 username username 0 Jan 15 13:09 b
-rw-r--r-- 1 username username 0 Jan 15 13:09 c
drwxr-xr-x 3 username username 4096 Jan 15 13:19 d
-rw-r--r-- 1 username username 153 Jan 15 13:19 md5sum.txt

/tmp/some_dir/d:
total 8
drwxr-xr-x 3 username username 4096 Jan 15 13:19 e
-rw-r--r-- 1 username username 0 Jan 15 13:10 g
-rw-r--r-- 1 username username 52 Jan 15 13:19 md5sum.txt

/tmp/some_dir/d/e:
total 8
drwxr-xr-x 2 username username 4096 Jan 15 13:19 f
-rw-r--r-- 1 username username 0 Jan 15 13:10 h
-rw-r--r-- 1 username username 0 Jan 15 13:19 j
-rw-r--r-- 1 username username 0 Jan 15 13:19 k
-rw-r--r-- 1 username username 162 Jan 15 13:19 md5sum.txt

/tmp/some_dir/d/e/f:
total 4
-rw-r--r-- 1 username username 0 Jan 15 13:10 i
-rw-r--r-- 1 username username 0 Jan 15 13:19 l
-rw-r--r-- 1 username username 112 Jan 15 13:19 md5sum.txt

$ cat /tmp/some_dir/d/md5sum.txt
d41d8cd98f00b204e9800998ecf8427e /tmp/some_dir/d/g

$ cat /tmp/some_dir/d/e/md5sum.txt
d41d8cd98f00b204e9800998ecf8427e /tmp/some_dir/d/e/h
d41d8cd98f00b204e9800998ecf8427e /tmp/some_dir/d/e/j
d41d8cd98f00b204e9800998ecf8427e /tmp/some_dir/d/e/k

teodorescup

I am a member!

Offline
Iscritto: 01/04/2011

Thank you ivaylo, but I'm afraid I haven't made myself understood (mostly because of the title).

The command you suggested seems to work fine but my problem now is how do I check the md5 files (created) from the subfolders.

I tried to modify the command myself but I can't get pass the pipe part; I only went as far as:
find /home/teodorescup/Muzică/m.lib/*/md5.txt
teodorescup

I am a member!

Offline
Iscritto: 01/04/2011

And, eventually, I found the string:

find /tmp/some_dir/ -type d | while read dir; do md5sum -c ${dir}/md5sum.txt ; done
Unfortunately, for some reason (probably the folder naming), I can't apply it for the task I need, yet.

ivaylo
Offline
Iscritto: 07/26/2010

В 23:28 +0100 на 15.01.2012 (нд), teodorescup[@nospam] написа:
> And, eventually, I found the string:
> find /tmp/some_dir/ -type d | while read dir; do md5sum -c ${dir}/md5sum.txt
> ; done
>
> Unfortunately, for some reason (probably the folder naming), I can't apply it
> to for the task I need, yet.

So you want to check the files in all sub directories against a md5sum
file in every sub directory?

The modification you've come up with seems to be working for me.
I don't know are you familiar with everything in my example and your
modification, so excuse me if you are.

The "/tmp/some_dir" is the top directory for the search. The "-type d"
searches for directories only. On every cycle of the while loop, "dir"
is the sub directory. If any directory has a white space symbol you
will have to put each ${dir} in quotes - "${dir}".

Magic Banana

I am a member!

I am a translator!

Offline
Iscritto: 07/24/2010

As far as you wrote, 'md5sum' was called from the "subfloders" and you need to do the same when checking. Moreover, double quotes are needed for paths including spaces. Here is something that should work if called from the root of the hierarchy subtree containing the md5.txt files:
$ find "$PWD" -name md5.txt | while read file; do cd "${file%/*}"; md5sum -c md5.txt; done

I can explain it to you if you wish. You probably want to add the --quiet option to 'md5sum' so that this command does not print "OK" for each successfully verified file.

teodorescup

I am a member!

Offline
Iscritto: 01/04/2011

@Magic Banana
Your string works for my 'problem', thank you.

@ivaylo
The problem was that the md5 files previously created by me didn't have the full path like the ones resulted from the string you proposed, and in consequence my modification was inefficient.

ivaylo
Offline
Iscritto: 07/26/2010

В 12:39 +0100 на 16.01.2012 (пн), teodorescup[@nospam] написа:

> @ivaylo
> The problem was that the md5 files previously created by me didn't have the
> full path like the ones resulted from the string you proposed, and in
> consequence my modification was inefficient.
>
>

Oh, that was the problem. In that case, you should change the working
directory to the sub directory, before running md5sum in your
modification - cd "${dir}". This is what Magic Banana's example is doing
too.

Magic Banana

I am a member!

I am a translator!

Offline
Iscritto: 07/24/2010

I have just realized there is a simpler way to achieve your goal:
$ find . -name md5.txt -execdir md5sum -c '{}' \;