tags:

views:

154

answers:

3

I'm trying to write a simple script that will list the contents found in two lists. To simplify, let's use ls as an example. Imagine "one" and "two" are directories.

one=`ls one`
two=`ls two`
intersection $one $two

I'm still quite green in bash, so feel free to correct how I am doing this. I just need some command that will print out all files in "one" and "two". They must exist in both. You might call this the "intersection" between "one" and "two".

+1  A: 

Use the comm command:

ls one | sort > /tmp/one_list
ls two | sort > /tmp/two_list
comm -12 /tmp/one_list /tmp/two_list

"sort" is not really needed but I alwasy include it before "comm" just in case.

DVK
+1  A: 

BASH FAQ entry #36

Ignacio Vazquez-Abrams
+2  A: 
comm -12  <(ls 1) <(ls 2)
ghostdog74