[TAG] Re: 2Cent Tip: Comparing all files in a directory

Benjamin A. Okopnik ben at linuxgazette.net
Sat Feb 25 02:06:08 MSK 2006


On Fri, Feb 24, 2006 at 04:48:06PM -0500, Suramya Tomar wrote:
> >>I just use 'diff', personally.
> >
> >I just hit 'Ctl-x d' in Midnight Commander. But Faber's method is
> >definitely more portable.
> >
> 
> Umm.. I did use diff to make the actual comparison, the script just 
> loops through each file in the current directory and runs a diff of it 
> against the same file name in the target directory.

Sure, that was very clear from your script. However, 'diff' is one of
those utilities that can take either files _or_ directories as
arguments. Here's an example of how it works:

``
ben at Fenrir:~$ mkdir /tmp/foo /tmp/bar
ben at Fenrir:~$ for n in {a..z}.txt; do cp /etc/hosts /tmp/foo/$n; done
ben at Fenrir:~$ cp /tmp/foo/{b..y}.txt /tmp/bar
ben at Fenrir:~$ echo >> /tmp/bar/y.txt 
ben at Fenrir:~$ echo >> /tmp/foo/b.txt 
''

So, I've just created a 'foo' and a 'bar' directory in '/tmp', created a
list of files in 'foo' (all copies of my '/etc/hosts'), copied the list
(except for two files) to 'bar', and slightly modified one file in each
directory. Ready? Here we go:

``
ben at Fenrir:~$ diff /tmp/foo /tmp/bar
Only in /tmp/foo: a.txt
diff /tmp/foo/b.txt /tmp/bar/b.txt
34d33
< 
diff /tmp/foo/y.txt /tmp/bar/y.txt
33a34
> 
Only in /tmp/foo: z.txt
ben at Fenrir:~$ 
''

'diff' shows the results of all the differences - both in the list of
files and in the files themselves. Pretty good little prog, hey? :)


* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://linuxgazette.net *





More information about the TAG mailing list