WebMasterCampus
WEB DEVELOPER Resources

Linux diff Command

Learn Linux div Command with examples


Linux diff Command

In Linux we can use “diff” command to display the differences in the files by comparing the files line by line. diff stands for difference.

Note: diff command uses certain special symbols and instructions that are required to make two files identical.

diff command Syntax

>> diff [options] File1 File2

Let’s create two files. fruits_1.txt and fruits_2.txt

The following comand will create fruits_1.txt file.

>> echo -e "Apple\nCherry\ngrapes\nMango\nKiwi\nstrawberry\nWatermelon" > fruits_1.txt

# File created with content
Apple
Cherry
grapes
Mango
Kiwi
strawberry
Watermelon

The following comand will create fruits_2.txt file.

>> echo -e "Apple\nApricot\nCherry\ngrapes\nLychee\nKiwi\nWatermelon" > fruits_2.txt

# File created with content
Apple
apricot
Cherry
grapes
Lychee
Kiwi
Watermelon

Basic diff command is run on two text files without any option, it will produces an output in the normal format.

diff command Example

>> diff file_1.txt file_2.txt


1a2
> apricot
4c5
< Mango
---
> Lychee
6d6
< strawberry

1a2, 4c5 and 6d6 are the part of diff command output of our files fruits_1.txt and fruits_2.txt that has specific meaning to understand.

Each change command contains the following, from left to right.

  1. The line number or range of lines in the first file like fruits_1.txt.
  2. A special change character like a, c or d.
  3. The line number or range of lines in the second file like fruits_2.txt.
Special Symbol Description
a add
c change
d delete
  • 1a2 - Add line 2 of the second file at the beginning of the file1 (after the line 1).
    • apricot - The line from the second line that is added to the first file as described above.
  • 4c5 - Replace (change) line 4 in the first file (Mango) with lines 4-5 (Lychee) from the second file.
  • 6d6 - Delete line 6 (strawberry) in the first file . The 6 after the d symbol means that if the line is not deleted it would appear on line 6 (strawberry) in the second file.

The output starts with the names and the timestamps of the files that are compared, and one or more sections that describe the differences.

Lines starting with two spaces are lines of context, the lines that are the same in both files.

  • + Plus symbol (+) are the lines that shows that the lines missing in the first file.
  • + Minus symbol (-) are the lines that shows that the lines missing in the second file.
  • ! Exclamation Mark symbol (!) are the lines that shows that the changed between two files. Each group of lines starting with ! from the first file has a corresponding match in the second file.

diff -c Produce Output in the Context Format

>> diff -c fruits.txt fruits2.txt

*** fruits_1.txt  2022-05-10 12:28:22.847819700 +0500
--- fruits_2.txt 2022-05-10 12:27:31.137819700 +0500
***************
*** 1,7 ****
  Apple
  Cherry
  grapes
! Mango
  Kiwi
- strawberry
  Watermelon
--- 1,7 ----
  Apple
+ apricot
  Cherry
  grapes
! Lychee
  Kiwi
  Watermelon

diff -u Produce Output in the Unified Format

>> diff -u fruits.txt fruits2.txt

--- fruits.txt  2022-05-10 12:28:22.847819700 +0500
+++ fruits2.txt 2022-05-10 12:27:31.137819700 +0500
@@ -1,7 +1,7 @@
 Apple
+apricot
 Cherry
 grapes
-Mango
+Lychee
 Kiwi
-strawberry
 Watermelon

The output begins with the names and the timestamps of the files and one or more sections that describe the differences.

diff -i Command Tells to Ignores Case

>> diff -ci fruits.txt fruits2.txt

*** fruits.txt  2022-05-10 12:28:22.847819700 +0500
--- fruits2.txt 2022-05-10 12:27:31.137819700 +0500
***************
*** 1,7 ****
  Apple
  Cherry
  grapes
! Mango
  Kiwi
- strawberry
  Watermelon
--- 1,7 ----
  Apple
+ apricot
  Cherry
  grapes
! Lychee
  Kiwi
  Watermelon

The diff command compares files line by line.

Comparing text files for differences is one of the most common tasks for administrators when updating/copying files.

diff Command in Linux (Documentation)

~$ man diff

NAME
       GNU diff - compare files line by line

SYNOPSIS
       diff [OPTION]... FILES

DESCRIPTION
       Compare FILES line by line.

       Mandatory  arguments  to long options are mandatory for short options
       too.

       --normal
              output a normal diff (the default)

       -q, --brief
              report only when files differ

       -s, --report-identical-files
              report when two files are the same

       -c, -C NUM, --context[=NUM]
              output NUM (default 3) lines of copied context

       -u, -U NUM, --unified[=NUM]
 -e, --ed
              output an ed script

       -n, --rcs
              output an RCS format diff

       -y, --side-by-side
              output in two columns

       -W, --width=NUM
              output at most NUM (default 130) print columns

       --left-column
              output only the left column of common lines

       --suppress-common-lines
              do not output common lines

       -p, --show-c-function
              show which C function each change is in

       -F, --show-function-line=RE
              show the most recent line matching RE
--label LABEL
              use LABEL instead of file name and timestamp (can be repeated)

       -t, --expand-tabs
              expand tabs to spaces in output

       -T, --initial-tab
              make tabs line up by prepending a tab

       --tabsize=NUM
              tab stops every NUM (default 8) print columns

       --suppress-blank-empty
              suppress space or tab before empty output lines

       -l, --paginate
              pass output through 'pr' to paginate it

       -r, --recursive
              recursively compare any subdirectories found

       --no-dereference
              don't follow symbolic links

       -N, --new-file
              treat absent files as empty
       
       --unidirectional-new-file
              treat absent first files as empty

       --ignore-file-name-case
              ignore case when comparing file names

       --no-ignore-file-name-case
              consider case when comparing file names

       -x, --exclude=PAT
              exclude files that match PAT

       -X, --exclude-from=FILE
              exclude files that match any pattern in FILE

       -S, --starting-file=FILE
              start with FILE when comparing directories

       --from-file=FILE1
              compare FILE1 to all operands; FILE1 can be a directory

       --to-file=FILE2
              compare all operands to FILE2; FILE2 can be a directory

       -i, --ignore-case
              ignore case differences in file contents
       -E, --ignore-tab-expansion
              ignore changes due to tab expansion

       -Z, --ignore-trailing-space
              ignore white space at line end

       -b, --ignore-space-change
              ignore changes in the amount of white space

       -w, --ignore-all-space
              ignore all white space

       -B, --ignore-blank-lines
              ignore changes where lines are all blank

       -I, --ignore-matching-lines=RE
              ignore changes where all lines match RE

       -a, --text
              treat all files as text

       --strip-trailing-cr
              strip trailing carriage return on input

       -D, --ifdef=NAME
              output merged file with '#ifdef NAME' diffs
      
      --GTYPE-group-format=GFMT
              format GTYPE input groups with GFMT

       --line-format=LFMT
              format all input lines with LFMT

       --LTYPE-line-format=LFMT
              format LTYPE input lines with LFMT

              These format options provide  fine-grained  control  over  the
              output

              of diff, generalizing -D/--ifdef.

       LTYPE is 'old', 'new', or 'unchanged'.
              GTYPE is LTYPE or 'changed'.

              GFMT (only) may contain:

       %<     lines from FILE1

       %>     lines from FILE2

       %=     lines common to FILE1 and FILE2

       %[-][WIDTH][.[PREC]]{doxX}LETTER
       printf-style spec for LETTER

              LETTERs  are  as  follows  for  new  group, lower case for old
              group:

       F      first line number

       L      last line number

       N      number of lines = L-F+1

       E      F-1

       M      L+1

       %(A=B?T:E)
              if A equals B then T else E

              LFMT (only) may contain:

       %L     contents of line

       %l     contents of line, excluding any trailing newline

       %[-][WIDTH][.[PREC]]{doxX}n
              printf-style spec for input line number
              
              Both GFMT and LFMT may contain:

       %%     %

       %c'C'  the single character C

       %c'\OOO'
              the character with octal code OOO

       C      the character C (other characters represent themselves)

       -d, --minimal
              try hard to find a smaller set of changes

       --horizon-lines=NUM
              keep NUM lines of the common prefix and suffix

       --speed-large-files
              assume large files and many scattered small changes

       --color[=WHEN]
              colorize the output; WHEN can be 'never', 'always', or  'auto'
              (the default)

       --palette=PALETTE
              the  colors  to  use  when  --color  is  active;  PALETTE is a
             colon-separated list of terminfo capabilities

       --help display this help and exit

       -v, --version
              output version information and exit

       FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or  'FILE  DIR'.
       If  --from-file  or  --to-file is given, there are no restrictions on
       FILE(s).  If a FILE is '-', read standard input.  Exit status is 0 if
       inputs are the same, 1 if different, 2 if trouble.
Created with love and passion.