Create md5 checksum files for each sub-directory

Posted on .

This bash script creates checksum files for each subdirectory and the files within.

If the script is run, and there are no changes, then there is no output. This makes it safe to put into a cronjob without fear of getting spammed.

Output can be forced with a “-d” flag.

Requires “md5sum” package to compute checksums.

Syntax:
md5-all-dir.sh [-d] <directory>

Download
  md5-all-dir.sh (4.1 KiB, 2,162 hits)

For example, say you have the following directory structure

$ tree
.
├── a
│   └── 1
├── b
│   ├── 1
│   └── 2
└── c
├── 1
├── 2
└── 3

3 directories, 6 files

This script will create a checksum file for directories “a”, “b” and “c”.

$ md5-all-dir ./
! Looping ./ ..
o ./c
- newfile: 1
- newfile: 2
- newfile: 3
o ./b
- newfile: 1
- newfile: 2
o ./a
- newfile: 1

So now the directory structure is:

$ tree
.
├── a
│   ├── 1
│   └── a-checksums.md5
├── b
│   ├── 1
│   ├── 2
│   └── b-checksums.md5
└── c
├── 1
├── 2
├── 3
└── c-checksums.md5

3 directories, 9 files

Each checksum file has the following structure:

$ cat ./c/c-checksums.md5
; Checksum created on Thu Jan 10 04:26:40 GMT 2013
; by [email protected] (/usr/local/bin/md5-all-dir)
; for /tmp/test/c

; data 1349606635 1349606635 10240 e91ce12bd9ee7bdeca70774f7b5fbea4 1
e91ce12bd9ee7bdeca70774f7b5fbea4 *1
; data 1349606641 1349606641 10240 e266420d33efae6d9f7e5e8804c48ed6 2
e266420d33efae6d9f7e5e8804c48ed6 *2
; data 1349606642 1349606642 10240 18ce779ed5275775f80053de68753cb5 3
18ce779ed5275775f80053de68753cb5 *3

; Time taken: 1 secs

The data line holds creation time, modified time, file size, md5sum and filename, and is used by the script to determine if any files have changed.

If the script is run again, it will parse the checksum files and update for any missing/modified/new files.

For example, if I add “a/testfile”:

$ touch ./a/testfile
$ md5-all-dir ./
! Looping ./ ..
o ./c
o ./b
o ./a
- newfile: testfile

Download
  md5-all-dir.sh (4.1 KiB, 2,162 hits)

Leave a Reply

You may leave the Name and Email fields blank to post anonymously.