Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have two big files (6GB each). They are unsorted, with linefeeds (\n) as separators. How can I diff them? It should take under 24h.

share|improve this question

2 Answers 2

up vote 19 down vote accepted

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.sorted

you could save creating an extra output file by piping the 2nd sort output direct into diff

sort a.file > a.file.sorted
sort b.file | diff --speed-large-files a.file.sorted -

Obviously these will run best on a system with plenty of available memory and you will likely need plenty of free disk space too.

It wasn't clear from your question whether you have tried these before. If so then it would be helpful to know what went wrong (took too long etc.). I have always found that the stock sort and diff commands tend to do at least as well as custom commands unless there are some very domain specific properties of the files that make it possible to do things differently.

share|improve this answer
+1. You can omit all temporary files with named pipes. Use mkfifo to create [ab].file.sorted before using them as output for sort. Put both sorts with & in the background and use the both piped as filenames for diff. –  krissi Sep 16 '10 at 11:45
@krissi You can also accomplish the same effect using this syntax: diff <(command 1) <(command 2) –  Michael Mrozek Sep 16 '10 at 14:12
Thanks worked. I needed a couple of GB of memory thought but a 16GB Amazon EC2 instance fixed it :) –  jonasl Sep 16 '10 at 15:28
If someone like me wonders why <(cmd1) <(cmd2) syntax works (as it sounds like redirecting standard input twice!), try echo hello <(cmd1) <(cmd2). You'll see something like hello /dev/fd/63 /dev/fd/62 which suddenly makes it clear ;) –  alex Sep 16 '10 at 19:53
In my experience, the --speed-large-files option does not help if you do not have enough RAM. Also, pre-sorting is not helpful if you have a multi-line record structure you wish to preserve. The options referred to above (by @unhammer) are interesting, but the output from rdiff and bsdiff is rather binary. Installing bdiff from the Heirloom Toolbox looks like a dauning task (requires Heirloom devtools, extinct header files, …). Is it really worth the effort? Are there other alternatives? –  Christian Pietsch Feb 2 at 17:33

Sorting the inputs and telling the diff program it's inputs are sorted would provide a massive speed up. I don't know of any diff with an option like that but comm assumes sorted input and will be much quicker if it does enough for your purposes.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.