You are currently viewing Linux Commands – diff

Linux Commands – diff

Linux Commands – diff

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed cat command in Linux which is used to print file content in standard output in Linux.

https://cloudaffaire.com/linux-commands-cat/

In this blog post, we will discuss diff command in Linux. diff stands for difference and in the simplest case, two file names FROM-FILE and TO-FILE are given, and diff compares the contents of FROM-FILE and TO-FILE. If one file is a directory and the other is not, diff compares the file in the directory whose name is that of the non-directory. The non-directory file must not be ‘-‘.

If two file names are given and both are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the ‘–recursive’ (‘-r’) option is given. diff never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of “file with the same name” does not apply. diff options begin with ‘-‘, so normally file names may not begin with ‘-‘. However, ‘–‘ as an argument by itself treats the remaining arguments as file names even if they begin with ‘-‘.

Linux Commands – diff:

diff command compares the contents of two files and outputs in standard output. The output format can be default one (–normal), context (–content) or unified (–unified). With –normal (default behavior of diff) format, the difference between the two files is printed. If both file contents are identical, diff returns nothing. If there is a difference, diff command will print the difference. In the 1st line in diff command, the normal output gives a summary of the type of change along with the line number where the change happened. line numbers can be clubbed together if there is a change in consecutive lines.

You can use diff -c or -C NUM or –context[=NUM] options to display the output in context format. In context format, 1st file creation time-stamp is printed followd by the differences where – (minus) represents delete, + (plus) represents add and ! (bang) represents change. *** <line_start,line_end> *** represents the source file and — <line_start,line_end> — represents the target file.

You can use diff -u or -U NUM or –unified[=NUM] options to output NUM (default 3) lines of unified context. In unified format, 1st file creation time-stamp is printed followed by the differences where – (minus) represents delete, + (plus) represents add and space represents no change. @@ <source_file_line_no> <target_file_line_no> @@

You can use diff -q or –brief options to report only when files differ. Does not displays the difference, only prints that the files differ.

You can use diff -s or –report-identical-files options to report when two files are the same. Returns the difference in normal format if the files differ.

You can use diff -e or -ed options to output the difference to an ed script and then use the ed script the make the source file same as target file.

You can use diff -n or –rcs options to output an RCS format diff used by RCS programs. like diff -f except that each command specifies the number of lines affected.

You can use diff -y or –side-by-side options to output in two columns. In the output, < denotes delete and > denotes add.

You can use diff -W or –width=NUM options to output at most NUM (default 130) print columns. This option is used with –side-by-side option to define the maximum column width.

You can use diff –left-column option to output only the left column of common lines in side by side format.

You can use diff –suppress-common-lines option to suppress common lines in the output.

You can use diff –label=LABEL option to display LABEL instead of file name and timestamp in the context format and unified format.

You can use diff -t or –expand-tabs options to expand tabs to spaces in the output, to preserve the alignment of tabs in the input files.

You can use diff -T or –initial-tab options to make tabs line up by prepending a tab.

You can use diff –tabsize=NUM option to tab stops every NUM (default 8) print columns.

You can use diff -l, or –paginate options to pass output through ‘pr’ to paginate it.

You can use diff command to compare the contents of directories.

You can use diff -r or –recursive options to recursively compare any subdirectories found.

You can use diff –no-dereference option to not follow symbolic links. The default behavior is to follow a symbolic link.

You can use diff -N or –new-file options to treat absent files as empty.

You can use diff –unidirectional-new-file option to treat absent first files as empty.

You can use diff –ignore-file-name-case option to ignore case when comparing file names.

You can use diff –no-ignore-file-name-case option to consider case (default behavior) when comparing file names. I am not aware of why this option exists since it’s the default behavior of diff. If you have some ideas, please write in the comment section.

You can use diff -x or –exclude=PATTERN options to exclude files that match PATTERN.

You can use diff -X or –exclude-from=FILE options to exclude files that match any pattern in FILE.

You can use diff -S or –starting-file=FILE options to start with FILE when comparing directories.

You can use diff –from-file=FILE1 option to compare FILE1 to all operands; FILE1 can be a directory.

You can use diff –to-file=FILE2 option to compare all operands to FILE2; FILE2 can be a directory.

You can use diff -i or –ignore-case options to ignore case differences in file contents.

You can use diff -E or –ignore-tab-expansion options to ignore changes due to tab expansion.

You can use diff -b or –ignore-space-change options to ignore changes in the amount of white space.

You can use diff -w or –ignore-all-space options to ignore all white space.

You can use diff -B or –ignore-blank-lines options to ignore changes where lines are all blank.

You can use diff -I RE or –ignore-matching-lines=RE options to ignore changes where all lines match RE. Ignore changes that just insert or delete lines that match RE.

Hope you have enjoyed this article. There are tons of options supported by diff command and due to time limitation, all option examples are not given in this blog post. If you want, you can give other options example in the comment section and we will collate all and include in this blog to make it the complete reference for Linux diff command.

In the next blog post, we will discuss more command in Linix.

 

Leave a Reply