So, I've run the ls -C1 on two directories:

mod@ux:~$ ls -C1 /bin | wc -l
mod@ux:~$ ls -C1 /sbin | wc -l

How to list only different filenames/commands and which directory contains them?

2 Answers 2


You can have it compare directly against the directory structure:

$ diff /sbin /bin|sort
Only in /bin: arping
Only in /bin: attr
Only in /bin: awk
Only in /bin: basename
Only in /sbin: agetty
Only in /sbin: arp
Only in /sbin: arptables-compat
Only in /sbin: badblocks

You can add recursion too by specifying -r, if you like. If you don't want to know about common subdirectories, add grep -v "Common subdirectories" to the pipeline.


Execute diff <(ls -C1 /bin |sort) <(ls -C1 /sbin |sort) |less.

With /bin and /sbin as input to the command above, diff will append < to files unique to /bin and > to files unique to /sbin. If you need the < and > to be more informative, you can use sed to replace them with more informative strings:

diff <(ls -C1 /bin |sort) <(ls -C1 /sbin |sort) |sed -e 's/^< /\/bin\//' -e 's/^> /\/sbin\//'

  • Thank you! The second command did the trick, just with one more sort at the end: diff <(ls -C1 /bin |sort) <(ls -C1 /sbin |sort) |sed -e 's/^< /\/bin\//' -e 's/^> /\/sbin\//' | sort
    – modlin
    Mar 4, 2018 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .