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
add comment

2 Answers

up vote 16 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  
+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
9  
@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
1  
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
    
Note that diff easily runs out of memory if you have really large files. For creating patches/deltas at least there are some other options: unix.stackexchange.com/a/77259/27186 though perhaps not for visually inspecting changes –  unhammer Mar 5 at 9:20
add comment

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
add comment

Your Answer

 
discard

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.