Cheat Sheet #day60 - cmp

Cheat Sheet #day60 - cmp

cmp Command Cheatsheet

The cmp command in Unix-like systems is used to compare two files byte by byte. It is useful for finding the first difference between two files. Here’s a quick reference guide:

Basic Syntax

cmp [OPTION]... FILE1 FILE2

Common Options

  • -b, --print-bytes: Output differing bytes in decimal format.

      cmp -b file1.txt file2.txt
    
  • -i POS, --ignore-initial=POS: Ignore the first POS bytes of each file.

      cmp -i 10 file1.txt file2.txt
    
  • -l, --line-by-line: Output the byte number and the differing bytes.

      cmp -l file1.txt file2.txt
    
  • -s, --silent, --quiet: Suppress all output, only return exit status.

      cmp -s file1.txt file2.txt
    
  • -v, --verbose: Output a message when files differ.

      cmp -v file1.txt file2.txt
    

Examples

  1. Compare two files and print the first difference:

     cmp file1.txt file2.txt
    
  2. Compare files byte by byte and print differing bytes in decimal format:

     cmp -b file1.txt file2.txt
    
  3. Ignore the first 10 bytes and compare the rest of the files:

     cmp -i 10 file1.txt file2.txt
    
  4. Output the byte number and differing bytes:

     cmp -l file1.txt file2.txt
    
  5. Silent comparison, only return exit status:

     cmp -s file1.txt file2.txt
    
  6. Verbose output when files differ:

     cmp -v file1.txt file2.txt
    

Additional Information

  • Help option:

      cmp --help
    
  • View cmp manual page:

      man cmp
    

This cheatsheet covers the essential options and usage scenarios for the cmp command. For more detailed information, refer to the man page or use cmp --help.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!