Linux Commands Examples

A great documentation place for Linux commands


compare files line by line

see also : cmp - diff3 - sdiff - patch


diff [OPTION]... FILES

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



How do I compare binary files in Linux?

There's a tool call DHEX which may do the job.

And there's another tool called VBinDiff

For a strictly command-line approach, try JDIFF


Binary diff/patch for large files on linux?

You should probably take a look at the rsync-related tools: rdiff and rdiff-backup. The rdiff command lets you produce a patch file and apply it to some other file.

The rdiff-backup command uses this approach to deal with entire directories, but I'm guessing you're working with single-file disk images, so rdiff will be the one to use.


How can I diff two XML files?

I use Beyond Compare to compare all types of text based files. They produce versions for Windows and Linux.


How do I do a binary diff on two identically sized files under Linux?

Found a solution - the cmp tool which comes with most Linux flavours.


How to diff large files on Linux

I found this link

diff -H might help, or you can try installing the textproc/2bsd-diff port which apparently doesn't try to load the files into RAM, so it can work on large files more easily.

I'm not sure if you tried those two options or if they might work for you. Good luck.


How to diff directories for different files, but not line-by-line

One way to do this is to do

diff dir1 dir1| grep "diff "

It will still do a line-by-line comparison, but each file comparison begins with "diff dir1/file dir2/file", so grepping "diff " will show only those lines ( i.e. the files that are different ).


Comparing 2 (or 3 Files If Possible) "Line By Line"

To compare files side-by-side, my favorite tool is Notepad++.

After installing it, on the toolbar click Plugins>Plugin Manager>Available, and check Compare and Install.

After restarting Notepad++, open up the two files (they will open in different tabs), and in the toolbar go to Plugins>Compare>Compare.

There are multiple tweaks you can do, but comparing files looks something like this:

enter image description here

where white text is unchanged, red is removed, green is added, and the sidebar shows an overview of those changes in the two files.


interactive/visual merge tool for console?

Ah just found out about vimdiff. It pretty much does what I was looking for. here's a quick guide


Listing lines from just one file in DIFF

Not sure diff alone can do it but you can always use the power of other GNU utilities to help you.

diff -u diffa.txt diffb.txt | grep '^-[^-]' | sed 's/^-//'

It does the diff, then selects only the lines that begins with '-' - those are changed and have values from diffa.txt file, then sed just remove those '-' signs.

Edit: After few experiments with diff, looks like the below command produces what you want:

diff --changed-group-format='%<' --unchanged-group-format='' diffa.txt diffb.txt

Difftool for Ubuntu like OS X's Opendiff

If your looking for command line diff would work wouldn't it? For graphical and not free I like Beyond Compare


Why are there binary differences among compressed files generated exactly the same way from the exact same starting file?

Two possible causes:

  • different compression algorithm used by the same compression program, or
  • different compression programs

Using diff on a long one line file

If this is a one-time problem, I'd create copies of the files with \n characters inserted every 50 characters, then diff those copies. (I chose 50 because it makes the math easy going from differing line number to byte offset in the original files but might adjust that up/down based on what I found.)


How to create Delta / Changed files from directories?

When using rsync, It seems that the -c (--checksum) option does not override the -t (--times) option within -a (--archive), which uses -rlptgoD, and thus it will still look at file modification times.

One possible solution is to use: -rlpgoDc rather than -a. The command would look like this:

cd Package_1.2
rsync -rlpgoDc --compare-dest=../Package_1.1 . ../changed_files/
find -depth ../changed_files -type d -empty -delete

This may not be the most elegant solution, perhaps someone has a better idea of how rsync can do the whole job by itself.


How to diff part of a file

Here you go:

diff <(head -n 1 file1) <(head -n 1 file2)

(this would return nothing what-so-ever).

diff <(head -n 2 file1) <(head -n 2 file2)

< 1
> 3

You could incorporate that into a script to do the things you mention.



diff <(head -n ${numLines} ${fileOne}) <(head -n ${numLines} ${fileTwo})

To use that, just make the script executable with chmod +x and then to execute, ./ ~/file1 ~/Docs/file2 That leaves the default # of lines at 1, if you want more append a number to the end of that command.

(Or you could do switches in your script with -f1 file1 -f2 file2 -n 1, but I don't recall of the top of my head the case statement for that).

head returns from the beginning the # of lines as suggested by -n. If you were to want to do reverse, it would be tail -n ${numLines} (tail does from the end back the number of lines).


Compare FILES line by line.

Mandatory arguments to long options are mandatory for short options too.

output a normal diff (the default)

-q, --brief

report only when files differ

-s, --report-identical-files

report when two files are the same

-c, -C NUM, --context[=NUM]

output NUM (default 3) lines of copied context

-u, -U NUM, --unified[=NUM]

output NUM (default 3) lines of unified context

-e, --ed

output an ed script

-n, --rcs

output an RCS format diff

-y, --side-by-side

output in two columns

-W, --width=NUM

output at most NUM (default 130) print columns


output only the left column of common lines


do not output common lines

-p, --show-c-function

show which C function each change is in

-F, --show-function-line=RE

show the most recent line matching RE

--label LABEL

use LABEL instead of file name (can be repeated)

-t, --expand-tabs

expand tabs to spaces in output

-T, --initial-tab

make tabs line up by prepending a tab


tab stops every NUM (default 8) print columns


suppress space or tab before empty output lines

-l, --paginate

pass output through ’pr’ to paginate it

-r, --recursive

recursively compare any subdirectories found

-N, --new-file

treat absent files as empty


treat absent first files as empty


ignore case when comparing file names


consider case when comparing file names

-x, --exclude=PAT

exclude files that match PAT

-X, --exclude-from=FILE

exclude files that match any pattern in FILE

-S, --starting-file=FILE

start with FILE when comparing directories


compare FILE1 to all operands; FILE1 can be a directory


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 RE

-a, --text

treat all files as text


strip trailing carriage return on input

-D, --ifdef=NAME

output merged file with ’#ifdef NAME’ diffs


format GTYPE input groups with GFMT


format all input lines with LFMT


format LTYPE input lines with LFMT

These format options provide fine-grained control over the output

of diff, generalizing -D/--ifdef.

LTYPE is ’old’, ’new’, or ’unchanged’.

GTYPE is LTYPE or ’changed’.

GFMT (only) may contain:


lines from FILE1


lines from FILE2


lines common to FILE1 and FILE2


printf-style spec for LETTER

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


first line number


last line number


number of lines = L-F+1






if A equals B then T else E

LFMT (only) may contain:


contents of line


contents of line, excluding any trailing newline


printf-style spec for input line number

Both GFMT and LFMT may contain:




the single character C


the character with octal code OOO


the character C (other characters represent themselves)

-d, --minimal

try hard to find a smaller set of changes


keep NUM lines of the common prefix and suffix


assume large files and many scattered small changes


display this help and exit

-v, --version

output version information and exit

FILES are ’FILE1 FILE2’ or ’DIR1 DIR2’ or ’DIR FILE...’ or ’FILE... DIR’. If --from-file or --to-file is given, there are no restrictions on FILE(s). If a FILE is ’-’, read standard input. Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.


Copyright © 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report bugs to: bug-diffutils[:at:]gnu[:dot:]org
GNU diffutils home page: <>
General help using GNU software: <>

see also

wdiff, cmp , diff3 , sdiff , patch

The full documentation for diff is maintained as a Texinfo manual. If the info and diff programs are properly installed at your site, the command

info diff

should give you access to the complete manual.


Written by Paul Eggert, Mike Haertel, David Hayes, Richard Stallman, and Len Tower.

How can this site be more helpful to YOU ?

give  feedback