• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

diff Command Examples in Linux

by admin

The diff command is used to compare text files. The command displays the two files and the differences between them. Using various symbols, the output suggests how you can change one file to make it identical to the other. Each symbol has a special meaning.

The less than symbol (<) with a line after it means that line should be removed from the first file because it doesn't appear in the second. The greater than symbol (>) with a line after it means that line should be added from the second file. In addition, the diff command also denotes the line numbers for each file that would be affected by deletion, addition, and change operations.

Syntax

The syntax of the diff command is:

# diff {file name 1} {file name 2}

diff COMMAND OPTIONS

The diff command has various options that enable you to specify the nature of the output.

Option Description
-b Ignore spacing differences.
-i Ignore case differences.
-t Expand tab characters in output lines.
-w Ignore spacing differences and tabs.
-c Display a list of differences with three lines of context.
-u Output results in unified mode, which presents a more streamlined format.

diff Command Examples

1. Compare files (lists changes to turn `old_file` into `new_file`):

# diff old_file new_file

2. Compare files, ignoring white spaces:

# diff --ignore-all-space old_file new_file

3. Compare files, showing the differences side by side:

# diff --side-by-side old_file new_file

4. Compare files, showing the differences in unified format (as used by `git diff`):

# diff --unified old_file new_file

5. Compare directories recursively (shows names for differing files/directories as well as changes made to files):

# diff --recursive old_directory new_directory

6. Compare directories, only showing the names of files that differ:

# diff --recursive --brief old_directory new_directory

7. Create a patch file for Git from the differences of two text files, treating nonexistent files as empty:

# diff --text --unified --new-file old_file new_file > diff.patch

8. diff can also compare directories:

# diff dir1 dir2

which compares any same-named files in those directories, and lists all files that appear in one directory but not the other. To compare entire directory hierarchies recursively, use the -r option:

# diff -r dir1 dir2

which produces a (potentially massive) report of all differences.

Conclusion

The diff command compares two files line-by-line, or two directories. When comparing text files, diff can produce detailed reports of their differences. For binary files, diff merely reports whether they differ or not. For all files, if there are no differences, diff produces no output.

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to Save Command history of Selected Users in Linux
  2. swapoff: command not found
  3. debsecan: command not found
  4. “error: Bind to port 2222 on 0.0.0.0 failed: Permission denied” – error while starting sshd service on CentOS/RHEL
  5. “sudo: /etc/sudoers is world writable” – How to correct the permissions of sudoers file
  6. How To Disable Or Extend System Logging Rate-limit on CentOS/RHEL 7
  7. CentOS / RHEL 7 : How to configure VLAN Tagging using nmcli
  8. Understanding /etc/group file
  9. How to Migrate CentOS/RHEL 6 iptables Rules to CentOS/RHEL 7 firewalld
  10. How To Create a Local Yum Repository for MySQL Enterprise Packages

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright