Hot answers tagged

83

do (diff obtain) and dp (diff put) is what you need. Here is a small list of other helpful commands in this context. ]c - advance to the next block with differences [c - reverse search for the previous block with differences do (diff obtain) - bring changes from the other file to the current file dp (diff put) - send changes ...


44

Try to use - to represent the standard input. ssh user@remote-host "cat path/file.name" | diff path/file.name -


39

There's wdiff, the word-diff for that. On desktop, meld can highlight the differences within a line for you.


34

Here's one workaround: diff seems to accept <(expr) as arguemnts: diff <(ssh \"cat path/file.name/") <(cat path/file.name)


30

Diffs can be more complicated than just comparing one file versus another. The can compare entire directory hierarchies. Consider the example that I want fix a bug in GCC. My change adds a line or two in 4 or 5 files and deletes a handful of lines in those and other files. If I want to communicate these changes to someone, potentially for inclusion into ...


26

The most obvious answer is just to use the diff command and it is probably a good idea to add the --speed-large-files parameter to it. diff --speed-large-files a.file b.file You mention unsorted files so maybe you need to sort the files first sort a.file > a.file.sorted sort b.file > b.file.sorted diff --speed-large-files a.file.sorted b.file....


26

Yes, it is possible. When using these options, the default is just to print out every line. This is very verbose, and not what you want. diff --unchanged-line-format="" will eliminate lines that are unchanged, so now only the old and new lines are produced. diff --unchanged-line-format="" --new-line-format=":%dn: %L" will now show the new lines ...


25

According to Gilles, the -I option only ignores a line if nothing else inside that set matches except for the match of -I. I didn't fully get it until I tested it. The Test Three files are involved in my test (take the indented code, ignore File test*; I did it this way to prevent formatting making it less readable): File test1: text File test2: ...


25

To understand the report, remember that diff is prescriptive, describing what changes need to be made to the first file (file1) to make it the same as the second file (file2). Specifically, the d in 1d0 means delete and the a in 2a2 means add. Thus: 1d0 means line 1 must be deleted in file1 (apples). 0 in 1d0 means line 0 is where they would have ...


24

Open the side by side view: Ctrl+w v Change between them: Ctrl+w h or l Checkout the vimdiff command, part of the vim package, if you want a diff-like view..


24

ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt Source


23

There are a number of tools that are usable: meld kompare -- diff file viewer kdiff3 -- file difference viewer Diffuse -- file difference viewer Do you have two files and want to view their differences? Use a "file difference viewer". Do you have a diff file and want to look at it in an easy-to-read display? Use a "diff file viewer".


20

You can also open vim in split-screen mode, with the -O option:- vim -O file1 [file2 ...] To then turn on diff mode, you need to run the :diffthis command in each pane. Another use-case scenario, is if you've already got one file open in vim, and you want to open and compare it against another. Then you can use the following vim commands:- :vs otherfile ...


20

$ alias diff='diff -W $(( $(tput cols) - 2 ))' ought to do it. You'll want to add it to ~/.bashrc as well. The - 2 is mainly paranoia, in case something (embedded double-width Unicode?) expands enough to make the line wrap; if you want, you can just use $ alias diff='diff -W $(tput cols)'


20

When you get a patch you can often (that is unless you have made changes to the exact same lines) apply the patch to a set of files that you have changed yourself as well. The patch has information about the old and the new state of the files. If you get a copied file you don't know what the original was (the old state) and you cannot apply the differences ...


20

My guess is you simply haven't sorted the files. That's one of the behaviors you can get on unsorted input: $ cat file1 foo bar $ cat file2 bar foo $ $ diff file1 file2 1d0 < foo 2a2 > foo But, if you sort: $ diff <(sort file1) <(sort file2) $ The diff program's job is to tell you whether two files are identical and, if not, where they ...


19

sort can be used to get the files into the same order so diff can compare them and identify the differences. If you have process substitution, you can use that and avoid creating new sorted files. diff <(sort file1) <(sort file2)


19

It's possible to do this without a plugin using the w command, so the buffer contents can be used in a shell command: :w !diff -au "%" - > changes.patch (% is substituted with the path of the file being edited, - reads the buffer from stdin)


17

How about using diff, even though you don't want a diff? Try this: diff --unchanged-group-format='@@ %dn,%df %<' --old-group-format='' --new-group-format='' \ --changed-group-format='' a.txt b.txt Here is what I get with your sample data: $ cat a.txt Foo Bar X Hello World 42 $ cat b.txt Foo Baz Hello World 23 $ diff --unchanged-group-format='@@...


17

You can use grep. Give it the small file as input and tell it to find non-matching lines: grep -vxFf file.txt bigfile.txt > newbigfile.txt The options used are: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) ...


17

If your shell supports process substitution, try: diff <(head -n 1 filea) <(head -n 1 fileb)


16

diff expects the names of two files, so you should put the two output on two files, then compare them: awk '{print $3}' f1.txt | sort -u > out1 awk '{print $2}' f2.txt | sort -u > out2 diff out1 out2 or, using ksh93, bash or zsh, you can fool diff with the command: diff <(awk '{print $3}' f1.txt | sort -u) <(awk '{print $2}' f2.txt | sort -u)


16

With GNU diffutils package's diff this will output only lines from file b which either were modified or newly inserted: diff --unchanged-line-format= --old-line-format= --new-line-format='%L' a b


16

The 2 diff tools I use the most would be meld and sdiff. meld Meld is a GUI but does a great job in showing diffs between files. It's geared more for software development with features such as the ability to move changes from one side to the other to merge changes but can be used as just a straight side-by-side diffing tool.      &...


14

I've used vimdiff for this. Here's a screenshot (not mine) showing minor one or two character differences that stands out pretty well. A quick tutorial too.


14

With GNU diff, pass one of the files as an argument to --from-file and any number of others as operand: $ diff -q --from-file file1 file2 file3 file4; echo $? 0 $ echo >>file3 $ diff -q --from-file file1 file2 file3 file4; echo $? Files file1 and file3 differ 1


14

Git uses isatty() to check whether stdout is a tty: this is used to see if a pager must be used (pager.c) as well as colors (color.c).


14

You can use the comm command to compare two files, and selectively show lines unique to one or the other, or the lines in common. It requires the inputs to be sorted, but you can sort them on the fly, by using process substitution. comm -13 <(sort old.txt) <(sort new.txt) If you're using a version of bash that doesn't support process substitution, ...


13

Might the differences be caused by DOS vs. UNIX line endings, or something similar? What if you hexdump them? This might show differences more obviously, eg: hexdump -C file1 > file1.hex hexdump -C file2 > file2.hex diff file1.hex file2.hex


13

diff cannot output colors, you need another program, such as colordiff for that. Colors in the terminal are printed via ANSI escape codes which less does not interpret by default. To get less to correctly show colors, you need the -r, or even better, -R switch: colordiff $file1 $file2 | less -R From man less: -R or --RAW-CONTROL-CHARS Like -...



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