What is the usage of the diff command in Linux?

The diff command is used to compare the content differences between two files, and can display the specific locations and content of the differences. Its basic usage is:

diff [options] file1 file2

Among them, file 1 and file 2 are the file paths to be compared.

Common options include:

  1. -b or –ignore-space-change: disregard differences in spaces.
  2. -c or –context: display differences in context format (default 3 lines of context)
  3. -u or –unified: display differences in a unified format (default showing 3 lines of context).
  4. -r or –recursive: compare files in directories recursively.
  5. -i or –ignore-case: disregard differences in letter case.
  6. -q or –brief: display only whether files are different, without showing specific differences.
  7. -display differences in regular format
  8. – side-by-side: display differences in a parallel manner
  9. -display-only-unique-lines: show only the lines that are different.

“I can’t attend the meeting tomorrow due to a scheduling conflict.”

“I won’t be able to make it to the meeting tomorrow because of a conflicting schedule.”

  1. Compare two files to identify specific differences in both content and location.
  2. Ignore differences in whitespace and display the specific differences in position and content between file1.txt and file2.txt.
  3. Compare files in two directories recursively:
    diff -r dir1 dir2
  4. Display differences in a unified format and show more context:
    diff -u5 file1.txt file2.txt

Please note that the diff command has more options and uses, which can be seen in detail by using the man diff command.

bannerAds