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 files which look identical to me (including trailing whitespaces and newlines) but diff still says they differ. Even when I do a diff -y side by side comparison the lines look exactly the same. The output from diff is the whole 2 files.

Any idea what's causing it?

share|improve this question
    
Try to compare unprintable characters. The simplest way to watch them is sed -n l filename. If it won't help, add a data example and diff output here. –  rush Aug 17 '12 at 13:18
    
Ahh yes thank you, the lines in a file are ending with $ and in the other one ending with \r$ –  MinaHany Aug 17 '12 at 13:33
add comment

4 Answers

up vote 6 down vote accepted

Odd .. can you try cmp? You may want to use the '-b' option too.

cmp man page - Compare two files byte by byte.

This is one of the nice things about Unix/Linux .. so many tools :)

share|improve this answer
    
Thanks for that! I got: byte 19, line 1 is 15 ^M 12 ^J what does it mean? –  MinaHany Aug 17 '12 at 13:22
    
looks like carriage return and linefeed according to this table –  Levon Aug 17 '12 at 13:25
add comment

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
share|improve this answer
    
Well, the two hexes are different. every time there's a 0d 0a in a file the other one just has 0a –  MinaHany Aug 17 '12 at 13:29
2  
In one, you have DOS line endings (CRLF) and in the other, UNIX line endings (LF). That's why they look different to diff but not when you look at them visually. Look at en.wikipedia.org/wiki/Newline#Conversion_utilities –  mrb Aug 17 '12 at 13:32
    
Got it! Thanks a lot. Levon's suggestion of using cmp shows the difference more clearly though :) –  MinaHany Aug 17 '12 at 13:39
add comment

My first guess, which turns out to be confirmed, is that the files use different line endings. It could be some other difference in whitespace, such as the presence of trailing whitespace (but you typically wouldn't get that on many lines) or different indentation (tabs vs spaces). Use a command that prints out whitespace and control characters in a visible form, such as

diff <(cat -A file1) <(cat -A file2)
diff <(sed -n l file1) <(sed -n l file2)

You can confirm that the differences only have to do with line endings by normalizing them first. You may have a dos2unix utility; if not, remove the extra CR (^M, \r, \015) character explicitly:

diff <(tr -d '\r' <file1) <(tr -d '\r' <file2)

or, if file1 is the one with DOS endings

 tr -d '\r' <file1 | diff - file2
share|improve this answer
add comment

Try:

diff file1 file2 | cat -t

The -t option will cause cat to show any special characters clearly - eg. ^M for CR, ^I for tab.

From the man page (OS X):

 -t      Display non-printing characters (see the -v option), and display tab characters as `^I'.

 -v      Display non-printing characters so they are visible.
         Control characters print as `^X' for control-X; the delete character
         (octal 0177) prints as `^?'.  Non-ASCII characters
         (with the high bit set) are printed as `M-' (for meta) followed by the
         character for the low 7 bits.
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.