docs:unix:diff

diff

  • compare files in two directories, don't show common lines, output in 2 columns and ignore white space differences
    diff -y -b --suppress-common path1 path2
  • compare all files recursively, excluding any with a .pdf extension, and only print file names that differ
    diff -r -q --exclude="*.pdf" path1 path2
  • compare all files recursively, excluding any with a .pdf extension, and only print file names that differ, but suppress common subdirectory and Only in lines
    diff -r -q --exclude="*.pdf" path1 path2 | grep -v -E '^Common subdirectories|^Only in'
  • unified
    diff --unified file1.txt file2.txt
  • context
    diff --context file1.txt file2.txt
  • diff -Naur oldfile newfile > patchfile
  • then, to patch an original, copy the patchfile beside the file to be updated, and patch it like this
    patch -p0 < patchfile

This outputs 2 columns, hiding lines that are the same, then pipes the result through the wc program with the flag to count newlines.

diff -y --suppress-common-lines file1.txt file2.txt | wc -l

The diff utility has a man page, but the more comprehensive documentation is available in info format (info diff).

  • docs/unix/diff.txt
  • Last modified: 2016/03/27 22:44
  • by billh