比較 comm, cmp, diff

最後更新: 2022-02-08

目錄


comm

 

Compare sorted files FILE1 and FILE2 line by line.

With no options, produce three-column output.

column

unique to FILE1 | unique to FILE2 | common to both files

Opt

       -1     suppress column 1 (lines unique to FILE1)
       -2     suppress column 2 (lines unique to FILE2)
       -3     suppress column 3 (lines that appear in both files)

Example    

       -12    Print only lines present in both file1 and file2 (即係得返 3)
       -23    Print lines in file1 not in file2 (得返 1)

Example

cat a.txt

1
2
3
4

cat b.txt

3
4
5
6

comm a.txt b.txt

1
2
                3
                4
        5
        6

 


cmp

 

compare two files byte by byte

cmp a.txt b.txt

a.txt b.txt differ: byte 1, line 1

Opts

-b  --print-bytes              # Print differing bytes.

cmp -b a.txt b.txt

a.txt b.txt differ: byte 1, line 1 is  61 1  63 3

-i SKIP  --ignore-initial=SKIP              Skip the first SKIP bytes of input.

-i SKIP1:SKIP2                                  Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.

 


diff

 

介紹

diff comparing the files line by line.

Unlike CLI "cmp" and "comm", it instructions(建立 change-command) that are required to make two files identical.

Usage

old.txt

a
b
c
d

new.txt

d
e
f
g

diff old.txt new.txt > patch.txt

cat patch.txt

1,3d0
< a
< b
< c
4a2,4
> e
> f
> g

change-command

[1]

1,3d0
  1. line numbers (ie. 4a) / range(ie. 1,3d) in the first file
  2. Special symbols are: (a: add c: change d: delete)
  3. line numbers / range corresponding to the second file match

old.txt 的第一至第三行刪除後就會 match new.txt 的 0 行 (0=開始)

< a
< b
< c
  • < removed from file
  • > added to the file

在 old.txt 分別刪除 a, b, c

[2]

4a2,4
> e
> f
> g

在 old.txt 原本的第4行後加上 e, f, g 去 match new.txt 的 第 2~4 行.

Useful Opts

-b, --ignore-space-change   # ignore changes in the amount of white space

-B, --ignore-blank-lines       # ignore changes where lines are all blank

-i  --ignore-case

-q                                     # Output only whether files differ.

-r                                     # Recursively compare any subdirectories found.

 

Exit status

  • 0 means no differences were found
  • 1 means some differences were found
  • 2 means trouble

Unified mode(-u)

The default output of diff intended to be read by a computer, not a human

Unified mode for human purposes

diff -u old.txt new.txt

--- old.txt     2022-02-08 12:25:31.559076225 +0800
+++ new.txt     2022-02-08 12:25:41.447038776 +0800
@@ -1,4 +1,4 @@
-a
-b
-c
 d
+e
+f
+g

Example: 對比目錄的不同

方法 1

diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt

  • -q      # Output only whether files differ.
  • -r      # Recursively compare any subdirectories found.

另外方法

Use find to list all the files in the directory then calculate the md5 hash for each file and pipe it to a file:

# cd /root/dir1

find public_html/ -type f -exec md5sum '{}' \; > /root/old.txt

# go to another folder ( cd /root/dir2 )

find public_html/ -type f -exec md5sum '{}' \; > /root/new.txt

# cd /root

diff old.txt new.txt

 


patch

 

apply a diff file to an original

patch [options] [originalfile [patchfile]]

option

  • -b, --backup
  • -c (--context), -e (--ed), -n (--normal)      # patch attempts to determine the type of the diff listing
  • --binary                                                  #  patches should be generated by diff --binary
  • --dry-run
  • -R, --reverse                                           # patch attempts to swap each hunk around before applying it.

Usage

patch old.txt patch.txt

patching file old.txt

 

 

Creative Commons license icon Creative Commons license icon