One day a shell command Linux text Operation series-diff command detailed _linux Shell

Source: Internet
Author: User
Tags diff

This is also a document comparison command, and mastering will save you a lot of work. Do you remember the comm order? Diff is more complex than he is, easy to use, not only to compare files, but also to compare directories. When a file has multiple versions, or more complex files, and directory comparisons, you expect a command that is more useful than Comm, and the diff is born for it.

Full Name: Different file

Instance:

File 1:v1.txt

Copy Code code as follows:

Hello
World V1_echo
V1_diff
V1_comm
Shell is easy!

File 2:v2.txt

Copy Code code as follows:

Hello world!
V2_echo
V1_diff
Easy shell!

1. General comparison:
$diff V1.txt V2.txt

Copy Code code as follows:

1,2c1,2
< Hello World
< V1_echo
---
> Hello world!
> V2_echo
4c4
< V1_comm
---
> Easy shell!

Lists the differences between the numbers, the number of rows, and C is the compare

2. Integrated output:

$diff-u v1.txt v2.txt > Version.patch

Copy Code code as follows:

-v1_echo
+hello world!
+v2_echo
V1_diff
-v1_comm
+easy shell!
Shell is easy!

"+" means the added line, "-" means the deleted line

3. Update cover V1.txt, note here borrowed patch

$patch –P1 v1.txt< Version.patch

4. Compare v1,v2 files, output only v2 changes

$diff-E V1.txt v2.txt

Copy Code code as follows:

4c
Easy shell!
.
1,2c
Hello world!
V2_echo
.

5. Comparison Catalogue

The passage of the book explains:

-N treats all the exact files as empty files
-A will be all files of municipal Party documents text
-U generates integrated output
-R traversal of all files under the directory

Compare all text information in two directories

$diff –naur Directory1 Directory2

This is not recommended unless you are certain that the two directories are basically the same.

6. After work fatigue, especially when doing a lot of things at the same time, will forget to have done upload, or modify a directory under the file. You can do this:

$diff V1.txt/${dir}

Directly compared to the same file, high intensity of the workers believe really have experience.

Interpretation of the-help

Too long to intercept a little part of the interpretation.

Copy Code code as follows:

Mandatory arguments to long options are mandatory for short options too.
--normal output a normal diff (the default)
-Q,--brief, only when files differ
-S,--report-identical-files when two files are the same
-C,-C num,--context[=num] Output NUM (default 3) lines of copied context
-U,-u num,--unified[=num] Output NUM (default 3) lines of unified context
-E,--ed output an ED script
-N,--rcs output an RCS format diff
-y,--side-by-side output in two columns

FILES can be in the form of ' FILE1 FILE2 ', ' DIR1 DIR2 ', ' DIR FILE ... ' or
' FILE ... DIR '.

Interpretation:

1.-q–s

-Q: Only two different is, tip "two files are different"

-S: Two files at the same time, prompt "Two files the same"

$diff-qs v1.txt V2.txt

(a bit of a boring order)

2.–y

Output in columns, adding to the visibility

Copy Code code as follows:

Hello World | Hello world!
V1_echo | V2_echo
V1_diff V1_diff
V1_comm | Easy shell!
Shell is easy! Shell is easy!

3. The format behind file

can be 1. File 2. Directories and directories 3. Files and directories 4. Directories and files
Explain the file in the form of a table of contents compared to a file with the same name as the comparison file
such as: $ diff v1.txt diff/
is actually the comparison between V1.txt and Diff/v1.txt. This is a very common function.

Contrast Comm
Compared to the file, Comm function is relatively simple, but also very intuitive, convenient
Diff provides a more powerful, more complex way of comparing, and can also compare directories and traverse directories.

Copy Code code as follows:

Usage: diff [Options] ... FILES
Compare FILES by line.

Mandatory arguments to long options are mandatory for short options too.
--normal output a normal diff (the default)
-Q,--brief, only when files differ
-S,--report-identical-files when two files are the same
-C,-C num,--context[=num] Output NUM (default 3) lines of copied context
-U,-u num,--unified[=num] Output NUM (default 3) lines of unified context
-E,--ed output an ED script
-N,--rcs output an RCS format diff
-y,--side-by-side output in two columns
-W,--width=num output at most NUM (default 130) Print columns
--left-column output only the left column of common lines
--suppress-common-lines do not output common lines

-P,--show-c-function show which C function the each change be in
-F,--show-function-line=re show the most recent line matching RE
--label label use label instead of file name
(Can be repeated)

-T,--expand-tabs expand tabs to spaces in output
-T,--initial-tab make tabs line up by prepending a tab
--tabsize=num tab stops every NUM (default 8) Print columns
--suppress-blank-empty Suppress space or tab before empty output lines
-L,--paginate Pass output through ' PR ' to paginate it

-R,--recursive recursively compare any subdirectories found
-N,--new-file treat absent files as empty
--unidirectional-new-file treat absent-A-empty
--ignore-file-name-case Ignore case when comparing file names
--no-ignore-file-name-case consider case when comparing file names
-X,--exclude=pat exclude files that match PAT
-X,--exclude-from=file exclude files that match no pattern in FILE
-S,--starting-file=file start with file when comparing directories
--from-file=file1 Compare FILE1 to all operands;
FILE1 can be a directory
--to-file=file2 Compare all operands to FILE2;
FILE2 can be a directory

-I,--ignore-case ignore case differences in file contents
-E,--ignore-tab-expansion ignore changes due to tab expansion
-Z,--ignore-trailing-space ignore white
-B,--ignore-space-change ignore changes in the amount
-W,--ignore-all-space Ignore all white spaces
-B,--ignore-blank-lines ignore changes whose lines are all blank
-I,--ignore-matching-lines=re ignore changes whose lines all match RE

-A,--text treat all files as text
--STRIP-TRAILING-CR strip trailing carriage return on input

-D,--ifdef=name output merged file with ' #ifdef NAME ' diffs
--GTYPE-GROUP-FORMAT=GFMT format Gtype input groups with GFMT
--LINE-FORMAT=LFMT format all input lines with LFMT
--LTYPE-LINE-FORMAT=LFMT format ltype input lines with LFMT
These format options provide fine-grained the output
of diff, Generalizing-d/--ifdef.
Ltype can be ' old ', ' new ' or ' unchanged '. Gtype can be a ltype choice.
or ' changed '.
GFMT (only) may contain:
%< lines from FILE1
%> lines from FILE2
%= lines common to FILE1 and FILE2
%[-][width][.[ Prec]]{doxx}letter Printf-style spec for letter
Letters are as follows for new group, lower case to old group:
F number
L last line number
N number of lines = l-f+1
E F-1
M l+1
% (a=b? t:e) if A equals B then T else E
LFMT (only) may contain:
%l contents of Line
%l contents of line, excluding any trailing newline
%[-][width][.[ Prec]]{doxx}n Printf-style spec for input line number
Both gfmt and lfmt may contain:
%%  %
%c ' C ' single character C
%c ' \ooo ' the character with octal code OOO
c the character C (other characters represent themselves)

-D,--minimal try hard to find a smaller set of changes
--horizon-lines=num Keep NUM lines of the common prefix and suffix
--speed-large-files assume large files and many scattered small changes

--help Display this Help and exit
-V,--version output version information and exit

FILES can be in the form of ' FILE1 FILE2 ', ' DIR1 DIR2 ', ' DIR FILE ... ' or
' FILE ... DIR '.
If--from-file or--to-file is given, there are no restrictions on file (s).
If FILE is '-', read the content from the standard input.
If the input is the same, the exit status is 0;1 to indicate that the input is different; 2 indicates an error occurred.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.