0% found this document useful (0 votes)
15 views

File Comparison Commands

The document outlines various Unix file comparison commands including 'cmp' for character-by-character comparison, 'comm' for comparing sorted files, and 'diff' for line-by-line comparison with detailed output formatting. It also mentions 'dircmp' for directory comparison and 'uniq' for filtering adjacent repeated lines in a file. Each command includes syntax and examples for better understanding.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

File Comparison Commands

The document outlines various Unix file comparison commands including 'cmp' for character-by-character comparison, 'comm' for comparing sorted files, and 'diff' for line-by-line comparison with detailed output formatting. It also mentions 'dircmp' for directory comparison and 'uniq' for filtering adjacent repeated lines in a file. Each command includes syntax and examples for better understanding.

Uploaded by

abhik.das84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Unix File Comparison Commands

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
involved 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

You might also like