Compare Directories and File Content in Linux without Dircmp

/* Posted February 17th, 2009 at 12:28pm */
/* Filed under How-To, Linux */

/* */

linux_abstract_msg

Dircmp is a great Linux tool that allows users to compare the directory contents of two similar directories in order to spot differences in between subdirectory structures or file content. However, dircmp is not a luxury afforded by all Linux distributions. CentOS, for example, does not include the dircmp tool and the source code is not easily available. Plus even if it was, who would want to spend time to build it when you can do the exact same thing with your trusty friend: diff.

Yes you heard right, diff isn’t just for files anymore. In fact, it’s quite useful to compare directory contents if you use it correctly. The important diff options to be aware of are:

  • –brief: This will not print out the actual differences between files as this level of detail is not necessary in a directory compare. Instead, brief merely points out the fact that there is a difference.
  • -r: This will recursively execute the diff so that you get a thorough comparison between the two directories

The full usage for dircmp with diff is:

% diff –brief –r dir1 dir2

As an example, let’s take two directories literally named dir1 and dir2. These are their file structures, starting with dir1:

dir1:
total 20
drwxrwxr-x 2 user user 4096 Feb 17 11:53 dir3
drwxrwxr-x 2 user user 4096 Feb 17 11:49 dir4
-rw-rw-r-- 1 user user 16 Feb 17 11:49 fileA.txt
-rw-rw-r-- 1 user user 460 Feb 17 11:50 fileB.txt
-rw-rw-r-- 1 user user 338 Feb 17 11:51 fileC.txt

dir1/dir3:
total 4
-rw-rw-r-- 1 user user 0 Feb 17 11:52 fileD.dat
-rw-rw-r-- 1 user user 2 Feb 17 11:53 fileE.dat

dir1/dir4:
total 0

Here is dir2:

dir1:
total 20
drwxrwxr-x 2 user user 4096 Feb 17 11:53 dir3
-rw-rw-r-- 1 user user 96 Feb 17 11:49 fileA.txt
-rw-rw-r-- 1 user user 460 Feb 17 11:50 fileB.txt
-rw-rw-r-- 1 user user 338 Feb 17 11:51 fileC.txt

dir1/dir3:
total 4
-rw-rw-r-- 1 user user 0 Feb 17 11:52 fileD.dat
-rw-rw-r-- 1 user user 0 Feb 17 11:53 fileE.dat

To sum up, dir1 contains dir4 which is not in dir2. Also the files fileA.txt and fileE.dat are different. Here is the result of the “dircmp” using the diff command:

Files dir1/dir3/fileE.dat and dir2/dir3/fileE.dat differ
Only in dir1: dir4
Files dir1/fileA.txt and dir2/fileA.txt differ

And there we have the results exactly as we have expect. The file fileE.at is different, dir4 only exists in dir1, and fileA.txt is different. The results of this output is more than sufficient to give a clear picture of the differences between two directories without resorting to dircmp.

Image/Flickr/K@#$0%

  • Twitter
  • Digg
  • del.icio.us
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Mixx
  • Yahoo! Buzz
  • NewsVine
  • Facebook
  • Google Bookmarks

Related Posts


Related Videos

Total Video Converter: The… A Great File Converter Free File Converter No… Actusoft Free DVD Ripper/…

Related Stories

Related Tweets

Leave a Reply

or Login (not required)





HTML tags allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>