Intersection of two lists in Bash

Solution with comm

comm is great, but indeed it needs to work with sorted lists. And fortunately here we use ls which from the ls Bash man page:

Sort entries alphabetically if none of -cftuSUX nor --sort.

comm -12  <(ls one) <(ls two)

Alternative with sort

Intersection of two lists:

sort <(ls one) <(ls two) | uniq -d

Symmetric difference of two lists:

sort <(ls one) <(ls two) | uniq -u

Bonus

Play with it ;)

cd $(mktemp -d) && mkdir {one,two} && touch {one,two}/file_{1,2}{0..9} && touch two/file_3{0..9}

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 always include it before using "comm" just in case.


A less efficient (than comm) alternative:

cat <(ls 1 | sort -u) <(ls 2 | sort -u) | uniq -d

comm -12  <(ls 1) <(ls 2)

Tags:

Bash