diff Command in Linux

Learn via video courses
Topics Covered

Overview

The diff command in Linux is a powerful utility that compares files or directories and displays the differences between them. It is commonly used by developers to identify changes in source code, configuration files, or documentation. This article provides an overview of the diff command, its syntax, and examples of its usage.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Linux diff Command Syntax

The syntax for the diff command is as follows:

Where:

  • OPTION: One or more options that modify the behavior of the diff command. These options can control how the differences are displayed or specify which files to compare.
  • FILES: Two files or directories to be compared by the diff command.

diff Command Options:

  • -c: Display output in context format, showing several lines of context around the differences.
  • -u: Display output in unified format, which is similar to context format but more compact.
  • -r: Recursively compare subdirectories when comparing directories.
  • -i: Ignore case differences when comparing files.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example Usages

  • Compare two text files and display the differences.:

    Output:

    Explanation: The diff command will compare the contents of file1.txt and file2.txt and show the differences between them line by line.

  • Compare two directories and display the differences in a unified format.:

    Output:

    Explanation: The diff command will compare the contents of dir1 and dir2 and show the differences between their files in a unified format.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Tips

  • Use the 'patch' command to apply the differences generated by the 'diff' command to a file.

  • You can pipe the output of 'diff' to other commands like 'less' for easier viewing.

Advanced Use Cases of diff Command in Linux

  • Compare two files ignoring case differences.:

    Output:

    Explanation: The diff command will compare the contents of file1.txt and file2.txt, ignoring case differences, and show the differences between them line by line.

  • Compare two directories recursively.:

    Output:

    Explanation: The diff command will compare the contents of dir1 and dir2, including their subdirectories, and show the differences between their files.

  • Compare two files and show the differences in context format.:

    Output:

    Explanation: The diff command will compare the contents of file1.txt and file2.txt and show the differences between them in context format, which includes several lines of context around the differences.

Conclusion

  • The diff command is a powerful utility for comparing files and directories.

  • It offers various options to customize the output format and comparison behavior.

  • It is widely used by developers and system administrators to identify changes in files or directories.