Overview of Unix File Comparison Commands :
In this tutorial, we will cover the different ways involved for comparing two files.
The file comparison command helps us to compare the files and find the similarities and differences between these files. The different file comparison commands used in Unix are cmp, comm, diff, dircmp, and uniq.
Unix Video #8:
Different ways of comparing two files in Unix
#1) cmp: This command is used to compare two files character by character.
- Syntax: cmp [options] file1 file2
- Example: Add write permission for user, group and others for file1.
- $ cmp file1 file2
#2) comm: This command is used to compare two sorted files.
- Syntax: comm [options] file1 file2
- One set of options allows selection of ‘columns’ to suppress.
- -1: suppress lines unique to file1 (column 1)
- -2: suppress lines unique to file2 (column 2)
- -3: suppress lines common to file1 and file2 (column3)
- Example: Only show column-3 that contains lines common between file1 and file2
- $ comm -12 file1 file2
#3) diff: This command is used to compare two files line by line.
- Description: The output indicates how the lines in each file are different, and the steps invoved to change file1 to file2. The ‘patch’ command can be used to make the suggested changes. The output is formatted as blocks of:
Change commands
< lines from file1
—
> lines from file2
The change commands are in the format [range][acd][range]. The range on the left may be a line number or a comma-separated range of line numbers referring to file1, and the range on the right similarly refers to file2. The character in the middle indicates the action i.e. add, change or delete.
- ‘LaR’ – Add lines in range ‘R’ from file2 after line ‘L’ in file1.
- ‘FcT’ – Change lines in range ‘F’ of file1 to lines in range ‘T’ of file2.
- ‘RdL’ – Delete lines in range ‘R’ from file1 that would have appeared at line ‘L’ in file2
- Syntax: diff [options] file1 file2
- Example: Add write permission for user, group and others for file1
- $ diff file1 file2
#4) dircmp: This command is used to compare the contents of directories.
- Description: This command works on older versions of Unix. In order to compare the directories in the newer versions of Unix, we can use diff -r
- Syntax: dircmp [options] dir1 dir2
- Example: Compare contents of dir1 and dir2
- $ dircmp dir1 dir2
#5) uniq: This command is used to filter the repeated lines in a file which are adjacent to each other
- Syntax: uniq [options] [input [output]]
- Example: Omit repeated lines which are adjacent to each other in file1 and print the repeated lines only once
- $ uniq file1
Also, check our upcoming tutorial to know more about File Manipulation!!