Previous: Summarizing Which Files Differ, Up: What Comparison Means [Contents][Index]
1.7 Binary Files and Forcing Text Comparisons ¶
If diff
thinks that either of the two files it is comparing isbinary (a non-text file), it normally treats that pair of files much asif the summary output format had been selected (see Summarizing Which Files Differ), andreports only that the binary files are different. This is because lineby line comparisons are usually not meaningful for binary files.This does not count as trouble, even though the resulting output doesnot capture all the differences.
diff
determines whether a file is text or binary by checking thefirst few bytes in the file; the exact number of bytes is systemdependent, but it is typically several thousand. If every byte inthat part of the file is non-null, diff
considers the file to betext; otherwise it considers the file to be binary.
Sometimes you might want to force diff
to consider files to betext. For example, you might be comparing text files that containnull characters; diff
would erroneously decide that those arenon-text files. Or you might be comparing documents that are in aformat used by a word processing system that uses null characters toindicate special formatting. You can force diff
to consider allfiles to be text files, and compare them line by line, by using the--text (-a) option. If the files you compare using thisoption do not in fact contain text, they will probably contain fewnewline characters, and the diff
output will consist of hunksshowing differences between long lines of whatever characters the filescontain.
You can also force diff
to report only whether files differ(but not how). Use the --brief (-q) option forthis.
In operating systems that distinguish between text and binary files,diff
normally reads and writes all data as text. Use the--binary option to force diff
to read and write binarydata instead. This option has no effect on a POSIX-compliant systemlike GNU or traditional Unix. However, many personal computeroperating systems represent the end of a line with a carriage returnfollowed by a newline. On such systems, diff
normally ignoresthese carriage returns on input and generates them at the end of eachoutput line, but with the --binary option diff
treatseach carriage return as just another input character, and does notgenerate a carriage return at the end of each output line. This can beuseful when dealing with non-text files that are meant to beinterchanged with POSIX-compliant systems.
The --strip-trailing-cr causes diff
to treat inputlines that end in carriage return followed by newline as if they endin plain newline. This can be useful when comparing text that isimperfectly imported from many personal computer operating systems.This option affects how lines are read, which in turn affects how theyare compared and output.
If you want to compare two files byte by byte, you can use thecmp
program with the --verbose (-l)option to show the values of each differing byte in the two files.With GNU cmp
, you can also use the -b or--print-bytes option to show the ASCII representation ofthose bytes. See Invoking cmp
, for more information.
If diff3
thinks that any of the files it is comparing is binary(a non-text file), it normally reports an error, because suchcomparisons are usually not useful. diff3
uses the same test asdiff
to decide whether a file is binary. As with diff
, ifthe input files contain a few non-text bytes but otherwise are liketext files, you can force diff3
to consider all files to be textfiles and compare them line by line by using the -a or--text option.