Suggest a StoryDo you have a news tip? We would like to hear from you.

Command Line | Compare File To Another Using cmp

cmp command become really handy to quickly check if 2 files are identical or what are the differences between these files in bytes number and which are the lines has any differences between both files.

Let’s take a look at an example compare between 2 ” text, video, script, …” files any kind of files with any extension.

first.txt, and second.txt

open command line interface “Terminal”

cmp -c first.txt second.txt

depending on the content of these two files, the output might become like this if there are any differences.

first.txt second.txt differ: byte 1, line 1 is 141 a 143 c
it means that the difference between both of them had a difference on byte 1 located on the line number 1.

if both files are identical it will take you to add a new command line.

Another Method

Compare the first file to the second file using bytes number.

cmp -l first.txt second.txt
1 141  12

cmp: EOF on 1.txt

These two lines shows the number of the byte in decimal and the different in octal value in the second line.

Here are more informations about cmp command, you can display it on the terminal using this command.

cmp --help

cmp […]

  • -b  –print-bytes  Print differing bytes.
  • -i SKIP  –ignore-initial=SKIP  Skip the first SKIP bytes of input.
  • -i SKIP1:SKIP2  –ignore-initial=SKIP1:SKIP2
  • Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.
  • -l  –verbose  Output byte numbers and values of all differing bytes.
  • -n LIMIT  –bytes=LIMIT  Compare at most LIMIT bytes.
  • -s  –quiet  –silent  Output nothing; yield exit status only.
  • -v  –version  Output version info.
  • –help  Output this help.
Support Us By Sharing

Leave a Reply

Enjoy LinuxNov? Please spread the word :)

  • +LinuxNov
  • LinuxNov
  • LinuxNov