Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I get information of a certain set of files in my mail every day, which looks like this:

58623208 Sep 14 20:08 blbn_blfbe_drv
57904920 Sep 14 19:54 blbn_cycmn
55814208 Sep 14 06:02 clsa_Upd
38912000 Sep 14 19:12 cs_chgpp
41107456 Sep 14 19:17 csmRFbe
39403520 Sep 14 19:09 csmUAddAct
39235584 Sep 14 19:20 csmUAddSub

... and so on, for about 60 files.

Now, I need to compare all these files, one by one, with the existing files to make sure that all the files are are found matching.

filename, timestamp, date, and size are the things should be matching.

Is there any way to automate it?

I was thinking about using either comm or diff.

share|improve this question
up vote 2 down vote accepted

I assume you have GNU date, with the -d option:

while read size mon day time filename; do
    if [[ ! -f "$filename" ]]; then
        echo "ERROR: no such file: $filename" >&2
        filesize=$(stat -c %s "$filename")
        if [[ "$size" != "$filesize" ]]; then
            echo "ERROR: size mismatch: $filename" >&2
            filetime=$(date -d "$(stat -c %y "$filename")" "+%b %d %H:%M")
            if [[ "$mon $day $time" != "$filetime" ]]; then
                echo "ERROR: date mismatch: $filename" >&2
share|improve this answer

Assuming that all new files are in (or can be moved to) new/ and all old files are in old/ directories, you can use 'diff -ur old/ new/' to see the differences in files between the directories.

If you don't care about the nature of the differences, only the fact that some files are different then use grep to filter the output - but you will lose the exit code from diff (1 = differences found). Instead you can use the exit code from grep (1 = no regexp matches found, which you can interpret to mean that all files are the same).

e.g. i generated some text files in old and new (just rubbish containing directory listings of /bin, /sbin, and /etc). then i edited new/baz and deleted some lines:

$ ls -l new old
total 28
-rw-r--r-- 1 cas cas 11063 Sep 15 15:27 baz
-rw-r--r-- 1 cas cas 14529 Sep 15 15:25 foo

total 20
-rw-r--r-- 1 cas cas  6843 Sep 15 15:24 bar
-rw-r--r-- 1 cas cas 12025 Sep 15 15:24 baz

first, compare the two dirs:

$ diff -ur old new | egrep '^(Only|diff)'
Only in old: bar
diff -ur old/baz new/baz
Only in new: foo
$ echo $?

here you can see the exit code when the src and dst dirs are the same (or have exactly the same content)

$ diff -ur old old | egrep '^(Only|diff)'
$ echo $?

you could also use cmp, but it can only match individual files, it can't recursively check two directories for differences. you'd have to write a for loop wrapper around it, and you'd have to take into account files that exist only in new but not in old. easier to use diff.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.