Live Chat!

Compare two directory listings

* * * * * 1 votos

November 8th, 2006 toydi Posted in Text Manipulation, comm, process substitution | Hits: 23011 |

Given two directories: ~/dir-new/ and ~/dir/,

$ ls ~/dir-new/
file-01.txt
file-02.txt
file-new.txt
$ ls ~/dir/
file-01.txt
file-02.txt
file-old.txt

To compare them:

comm <(ls ~/dir-new/) <(ls ~/dir)

comm compares two files line-by-line and displays 3 columns : 1) lines unique to first file, 2) lines unique to second file, 3) lines appear in both files.

                           file-01.txt
                           file-02.txt
file-new.txt
              file-old.txt

To remove Column 1, try 'comm -1', and 'comm -23' will remove Column 2 and 3.

And this, '<(ls ~/dir-new/)', they call it as process substitution. Since comm only reads from files, '<( )' pretends itself as a file by returning a filename, which is read-only, containing the output of 'ls ~/dir-new/'.

In short, we may think '<(commands)’ as a pseudo, read-only file:

$ ls -l <(true)
lr-x------ 1 toydi toydi 64 2006-11-08 14:30 /dev/fd/63 -> pipe:[28600]

2 Responses to “Compare two directory listings”

  1. diff dir dir-new

    give er a try!

  2. i like the ‘comm’ command, diff was giving me grief trying to find unique files between 2 dirs so i could copy/sync things up.

    cheers for this tip!

Leave a Reply