Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
103 votes

How to color diff output?

diff --color option was added to GNU diffutils 3.4 (2016-08-08) This is the default diff implementation on most distros, which will soon be getting it. Ubuntu 18.04 has diffutils 3.6 and therefore has ...
Ciro Santilli OurBigBook.com's user avatar
102 votes
Accepted

Common lines between two files

Use comm -12 file1 file2 to get common lines in both files. You may also needs your file to be sorted to comm to work as expected. comm -12 <(sort file1) <(sort file2) From man comm: -1 ...
αғsнιη's user avatar
  • 41.4k
73 votes

diff within a line

Just another method using git-diff: git diff -U0 --word-diff --no-index -- foo bar | grep -v ^@@ grep -v if not interested in positions of the diffs.
Deepak's user avatar
  • 831
68 votes
Accepted

How does "diff --exclude" work?

The -x or --exclude options for GNU diff take a filename globbing pattern that the name of each file and directory will be matched against. If the pattern matches a particular name, that name is ...
Kusalananda's user avatar
  • 336k
56 votes

How to compare two different files line by line in unix?

Use diff command as following, in bash or any other shell that supports <(...) process substitutions or you can emulate it as shown here: diff --new-line-format='FALSE'$'\n' \ --old-line-...
αғsнιη's user avatar
  • 41.4k
41 votes
Accepted

Compare directories but not content of files

rsync compares only metadata by default. rsync -n -a -i --delete source/ target/ explanation: -n compare but do not actually copy or delete <-- THIS IS IMPORTANT!!1 -a compare all metadata -i ...
Lesmana's user avatar
  • 27.5k
39 votes
Accepted

How shall I understand the unified format of diff output?

I suspect the first paragraph (of the two you highlight) attempts to explain from-file-line-numbers whereas the second one attempts to explain to-file-line-numbers. I’ll ignore the text, which is ...
Stephen Kitt's user avatar
32 votes

How do I pipe colored diff output to less?

The other answers here might be out of date. As of coreutils 3.5 diff can indeed produce colored output which is turned off by default when the stdout is not a console. From the man page: --color[...
Kshitiz Sharma's user avatar
32 votes
Accepted

How to write the difference between two files into a file

comm -1 -3 a.txt b.txt > c.txt The -1 excludes lines that are only in a.txt, and the -3 excludes lines that are in both. Thus only the lines exclusively in b.txt are output (see man comm or comm -...
cas's user avatar
  • 78.9k
29 votes

Compare two zip files for differences

You will have to unzip them (if only in memory) to compare the two. A cool way I have seen to do this with diff is: diff -y <(unzip -l file1.zip) <(unzip -l file2.zip) That will show you if ...
Jaken551's user avatar
  • 565
28 votes
Accepted

Using the diff command to compare two strings?

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files. A shortcut way to do that is using process substitutions in a shell that supports these: ...
Kusalananda's user avatar
  • 336k
26 votes

How to compare two different files line by line in unix?

Assuming the files contain no tab-characters: $ paste file1 file2 | awk -F '\t' '{ print ($1 == $2 ? "TRUE" : "FALSE") }' TRUE TRUE FALSE FALSE This uses paste to create two tab-delimited columns, ...
Kusalananda's user avatar
  • 336k
23 votes

Is there a condensed side-by-side diff format?

Nobody mentioned icdiff yet? It's great! Pic speaks for itself:
Radek Postołowicz's user avatar
22 votes

Comparing two files in Vim

Or just open the first file in VIM, then :vert diffsplit file2 :vert makes it split the screen vertically. diffsplit does a diff, and splits the files and scrolls locks them.
Dustin's user avatar
  • 221
22 votes
Accepted

Diff of two similar big raw binary files

For the second application/issue, I would use a deduplicating backup program like restic or borgbackup, rather than trying to manually keep track of "patches" or diffs. The restic backup program ...
Kusalananda's user avatar
  • 336k
21 votes
Accepted

Space not taken as an argument separator by shell script (could someone please explain that small file difference ?)

c2 a0 is the UTF-8 encoding of the non-breaking space character. It usually looks like a regular space, but isn't recognized as whitespace by the shell. In a few keymaps, something like AltGr+Space, ...
ilkkachu's user avatar
  • 140k
19 votes

How to fix "Hunk #1 FAILED at 1 (different line endings)" message?

I had the same problem using the patchcommand that comes with MSYS2 on Windows. In my case both the source file and the patch had CRLF line-ending, and converting both to LF didn't work either. What ...
Fernando Costa Bertoldi's user avatar
19 votes

diff - how to ignore empty lines

Use the -B switch: -B --ignore-blank-lines Ignore changes whose lines are all blank. To ignore whitespaces, use the -b and -w switches: -b --ignore-space-change Ignore changes in the amount of ...
13dimitar's user avatar
  • 1,539
19 votes

Find files that have the same name as the directory

You can easily match file names that have the same name as the containing directory with a regex using a backreference. These are file names of the form what/ever/something/something where the two ...
Gilles 'SO- stop being evil''s user avatar
18 votes
Accepted

Elegant way of diff'ing two variables?

echo. Clearly less weird. #!/bin/bash a="`seq 10`" b="`seq 0 11`" diff <(echo "$a") <(echo "$b")
Ole Tange's user avatar
  • 35.6k
17 votes

How to get character level difference using "diff" command in Linux using shell script?

Short answers from here: git diff --word-diff=color --word-diff-regex=. file1 file2 And here: diff -u file1 file2 |perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight
Cees Timmerman's user avatar
16 votes

Is `git diff` related to `diff`?

The file format is interoperable. Git uses the best format, diff -u. It also extends it to represent additional types of changes. The equivalent to patch is git apply. It stages the changes in the ...
sourcejedi's user avatar
  • 50.6k
16 votes
Accepted

I want to compare values of two files, but not based on position or sequence

Compare the sorted files. In bash (or ksh or zsh), with a process substitution: diff <(sort File1.txt) <(sort File2.txt) In plain sh: sort File1.txt >File1.txt.sorted sort File1.txt >...
Gilles 'SO- stop being evil''s user avatar
15 votes
Accepted

diff showing only differences inside line

Using wdiff: $ wdiff -3 a.txt b.txt ====================================================================== [-b-] {+B+} ====================================================================== The -3 ...
Kusalananda's user avatar
  • 336k
14 votes
Accepted

Recursively compare directories with summary on different contents without examining file contents' differences

Usually this looks already good: diff -rq dirA dirB
rudimeier's user avatar
  • 10.4k
14 votes
Accepted

What effect does the "-d" option have with diff?

In GNU diff, also used on FreeBSD, the --minimal flag triggers an algorithm variation by Paul Eggert that causes it "to limit the cost to O(N**1.5 log N) at the price of producing suboptimal output ...
JdeBP's user avatar
  • 69.1k
13 votes

Understanding of diff output

1) Rename the file parameters to help you remember what's going on like this: Rather than: diff f1 f2 # f1=file 1, and f2=file2 think diff file-to-edit file-with-updates The results by default ...
Elliptical view's user avatar
13 votes

How to color diff output?

Try colordiff file1 file2 Availability of colordiff with your Linux/BSD distribution Those running Debian or Ubuntu (or any of their derivatives) can probably just use "apt-get install colordiff&...
modesto's user avatar
  • 241
12 votes

Converting colored output into html

ansi2html, on pypi. One may derive immoderate amounts of enjoyment from piping regular shell output through lolcat then through ansi2html. Some thing like: … | lolcat -f | ansi2html -ip
scruss's user avatar
  • 314

Only top scored, non community-wiki answers of a minimum length are eligible