How to Compare 2 folders or 2 files in the Command Line

One can easily compare the difference of files in two folders on the Command Line using diff command.

With folders we use rq as an argument and pass in the two folder names on the command line

diff -rq folder1 folder2

This will notify which corresponding files are different, and which files are unique in each folder. Once you know what files are different you then can run the diff file command on those files to see the actual difference.

With files one can pass below in to the command line with diff

diff file1.txt file2.txt

Commonly-Used diff Options

Here are some useful diff options to take note of:

-b Ignore any changes which only change the amount of whitespace (such as spaces or tabs).
-w Ignore whitespace entirely.
-B Ignore blank lines when calculating differences.
-y Display output in two columns.

These are only some of the most commonly-used diff options. What follows is a complete list of diff options and their function.

diff syntax

diff [OPTION]... FILES


–normal output a “normal” diff. (This is the default)
-q, –brief Produce output only when files differ. If there are no differences, output nothing.
-s, –report-identical-files Report when two files are the same.
-c, -C NUM, –context[=NUM] Provide NUM (default 3) lines of context.
-u, -U NUM, –unified[=NUM] Provide NUM (default 3) lines of unified context.
-e, –ed Output an ed script.
-n, –rcs Output an RCS-format diff.
-y, –side by side Format output in two columns.
-W, –width=NUM Output at most NUM (default 130) print columns.
–left-column Output only the left column of common lines.
–suppress-common-lines Do not output lines common between the two files.
-p, –show-c-function For files that contain C code, also show which C function each change is in.
-F,–show-function-line=RE Show the most recent line matching regular expression RE.
–label LABEL When displaying output, use the label LABEL instead of the file name. This option can be issued more than once for multiple labels.
-t, –expand-tabs Expand tabs to spaces in output.
-T, –initial-tab Make tabs line up by prepending a tab if necessary.
–tabsize=NUM Define a tab stop as NUM (default 8) columns.
–suppress-blank-empty Suppress spaces and/or tabs before empty output lines.
-l, –paginate Pass output through pr to paginate it.
-r, –recursive Recursively compare any subdirectories found.
-N, –new-file If a specified file does not exist, perform the diff as if it is an empty file.
–unidirectional-new-file Same as -n, but only applies to the first file.
–ignore-file-name-case Ignore case when comparing file names.
–no-ignore-file-name-case Consider case when comparing file names.
-x, –exclude=PAT Exclude files that match filename pattern PAT.
-X, –exclude-from=FILE Exclude files that match any filename pattern in file FILE.
-S, –starting-file=FILE Start with file FILE when comparing directories.
–from-file=FILE1 Compare FILE1 to all operands; FILE1 can be a directory.
–to-file=FILE2 compare all operands to FILE2; FILE2 can be a directory.
-i, –ignore-case ignore case differences in file contents.
-E, –ignore-tab-expansion ignore changes due to tab expansion.
-b, –ignore-space-change ignore changes in the amount of white space.
-w, –ignore-all-space ignore all white space.
-B, –ignore-blank-lines ignore changes whose lines are all blank.
-I, –ignore-matching-lines=RE ignore changes whose lines all match regular expression RE.
-a, –text treat all files as text.
–strip-trailing-cr strip trailing carriage return on input.
-D, –ifdef=NAME output merged file with “#ifdef NAME” diffs.
GTYPE-group-format=GFMT format GTYPE input groups with GFMT.
–line-format=LFMT format all input lines with LFMT.
LTYPE-line-format=LFMT format LTYPE input lines with LFMT.

These format options provide fine-grained control over the output ofdiff, generalizing -D/–ifdef.

LTYPE is old, new, or unchanged.

GTYPE can be any of the LTYPE values, or the value changed.

GFMT (but not LFMT) may contain:

%< lines from FILE1
%> lines from FILE2
%= lines common to FILE1 and FILE2.
%[][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER

LETTERs are as follows for new group, lower case for old group:

F first line number
L last line number
N number of lines = LF + 1
E F1
M L + 1
%(A=B?T:E) if A equals B then T else E

LFMT (only) may contain:

%L contents of line
%l contents of line, excluding any trailing newline
%[][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number

Both GFMT and LFMT may contain:

%% A literal %
%c’C the single character C
%c’\OOO’ the character with octal code OOO
C the character C (other characters simply represent themselves)
-d, –minimal try hard to find a smaller set of changes.
–horizon-lines=NUM keep NUM lines of the common prefix and suffix.
–speed-large-files assume large files and many scattered small changes.
–help display a help message and exit.
-v, –version output version information and exit.

FILES takes the form “FILE1 FILE2” or “DIR1 DIR2” or “DIR FILE…” or “FILEDIR“.

If the –from-file or –to-file options are given, there are no restrictions on FILE(s). If aFILE is a dash (““), diff reads from standard input.

Exit status is either 0 if inputs are the same, 1 if different, or 2 if diff encounters any trouble.

'Coz sharing is caring

By Swatantra Kumar

Swatantra is an engineering leader with a successful record in building, nurturing, managing, and leading a multi-disciplinary, diverse, and distributed team of engineers and managers developing and delivering solutions. Professionally, he oversees solution design-development-delivery, cloud transition, IT strategies, technical and organizational leadership, TOM, IT governance, digital transformation, Innovation, stakeholder management, management consulting, and technology vision & strategy. When he's not working, he enjoys reading about and working with new technologies, and trying to get his friends to make the move to new web trends. He has written, co-written, and published many articles in international journals, on various domains/topics including Open Source, Networks, Low-Code, Mobile Technologies, and Business Intelligence. He made a proposal for an information management system at the University level during his graduation days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.