How can I output the difference between 2 files?

Given two files containing unsorted lists of users, e.g.

In file1:
    userD
    user3
    userA
    user1
    userB

and

In file2:
    user3
    userB
    userX
    user1

then to get a simple list of the users in file1 but not in file2, you can do

$ comm -23 <(sort file1) <(sort file2)
userA
userD

and similarly to get the users in file2 but not in file1

$ comm -13 <(sort file1) <(sort file2)
userX

If the list files are already sorted, these can be simplified to comm -23 file1 file2 and comm -13 file1 file2 respectively.


The utility you're looking for is diff. Take a peek at the manual for details.


The best command to view the difference in the files content would be

vim -d file1 file2