diff Command in Linux with Examples

Posted in

diff Command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on November 21, 2024

    Comparing multiple files can be a helpful task in Linux that we wish to automate. In this article, we are going to discuss the diff command that allows you to line-by-line compare two files and its options.

    diff command in Linux

    diff stands for the difference. diff is a command-line tool that allows you to line-by-line compare two files. It shows us where lines in one file need to be modified to make the two files identical, unlike its companion members cmp and comm. It can also compare directories' contents.

    The most crucial thing to remember is that diff employs a set of special symbols and instructions to make two files similar. It explains how to edit the first file to make it match the second. The diff command is typically used to build a patch that contains the differences between one or more files and may be applied with the patch command. The syntax is:

    diff [OPTION]... FILE1 FILE2…

    Let's say we have two files with the names demo1.txt and demo2.txt containing some names of cities or states.

    $ ls
    
    demo1.txt demo2.txt
    
    $ cat demo1.txt
    
    Gujarat
    
    Uttar Pradesh
    
    Kolkata
    
    Bihar
    
    Jammu and Kashmir
    
    $ cat demo2.txt
    
    Tamil Nadu
    
    Gujarat
    
    Andhra Pradesh
    
    Bihar
    
    Uttar pradesh

    Now, applying the diff command without any option, we get the following output:

    $ diff demo1.txt demo2.txt
    
    0a1
    
    > Tamil Nadu
    
    2,3c3
    
    < Uttar Pradesh
    
     Andhra Pradesh
    
    5c5
    
     Uttar pradesh

    The output of the command will contain the following things that we need to understand:

    • Line numbers corresponding to the first file,
    • Line numbers corresponding to the second file.
    • Lines beginning with < are from the first file.
    • Lines beginning with > are from the second file.
    • The three dashes ("—") are just separators between the lines of files 1 and 2.
    • There are three special symbols in the output of the command. These symbols indicate how the first file needs to be edited to match the second file. The output will display either of the following:
    • a (add)
    • c (change)
    • d (delete)

    Options with the diff command

    1. -c (context)

    Use the -c option to see differences in context mode.

    $ cat demo1.txt
    
    cat
    
    mv
    
    comm
    
    cp 
    
    $ cat demo2.txt
    
    cat
    
    cp
    
    diff
    
    comm
    
    $ diff -c demo1.txt demo2.txt
    
    *** demo1.txt Thu Jan 11 08:52:37 2018
    
    --- demo2.txt Thu Jan 11 08:53:01 2018
    
    ***************
    
    *** 1,4 ****
    
    cat
    
    - mv
    
    - comm
    
    cp
    
    --- 1,4 ----
    
    cat
    
    cp
    
    + diff
    
    + comm

    2. -u (unified)

    Use the -u option to display differences in unified mode. It's similar to context mode, except it doesn't reveal any superfluous information and presents data in a succinct manner.

    $ cat demo1.txt
    
    cat
    
    mv
    
    comm
    
    cp 
    
    $ cat demo2.txt
    
    cat
    
    cp
    
    diff
    
    comm
    
    $ diff -u demo1.txt demo2.txt
    
    --- file1.txt 2018-01-11 10:39:38.237464052 +0000
    
    +++ file2.txt 2018-01-11 10:40:00.323423021 +0000
    
    @@ -1,4 +1,4 @@
    
     cat
    
    -mv
    
    -comm
    
     cp
    
    +diff
    
    +comm

    3. -i

    This command is case-sensitive by default. Use the -i option with diff to make this command case insensitive.

    $ cat demo1.txt
    
    dog
    
    mv
    
    CP
    
    comm
    
    $ cat demo2.txt
    
    DOG
    
    cp
    
    diff
    
    comm

    Without using this option:

    $ diff demo1.txt demo2.txt
    
    1,3c1,3
    
    < dog
    
    < mv
    
     DOG
    
    > cp
    
    > diff
    
    Using this option:
    
    $ diff -i demo1.txt demo2.txt
    
    2d1
    
     diff

    4. –version

    This option is used to show the diff version that is presently executing on your system.

    $ diff --version
    
    diff (GNU diffutils) 3.5
    
    Packaged by Cygwin (3.5-2)
    
    Copyright (C) 2016 Free Software Foundation, Inc.
    
    License GPLv3+: GNU GPL version 3 or later .
    
    This is free software: you are free to change and redistribute it.
    
    There is NO WARRANTY, to the extent permitted by law.


    4. —help

    This command displays the various options and helps related to the diff command.

    Conclusion

    In this article, we talked about the diff command and its options which are used to see differences in context mode, to display differences in unified mode, to make this command case insensitive, and to show the diff version that is presently executing on your system.

    People are also reading :

    Leave a Comment on this Post

    0 Comments