[root@linux ~]# diff [-bBiqn] from-file to-file 選項: from-file :檔名,作為原始比對檔案的檔名; to-file :檔名,作為目的比對檔案的檔名; # from-file 或 to-file 可以 - 取代, - 代表『Standard input』。 -b :忽略一行當中,多個空白的差異 (例如 "about me" 與 "about me" 視為相同) -B :忽略空白行的差異。 -i :忽略大小寫的不同。 -q :只列出檔案是否有差異。 -n :以 RCS 格式輸出檔案之差異。 -c (-C NUM) :兩個檔案皆加入差異部分前後 NUM 行,以增加輸出之可讀性。預設 NUM=3。 -u (-U NUM) :加入差異部分前後 NUM 行,以增加輸出之可讀性。預設 NUM=3。
[root@linux ~]# mkdir -p /tmp/test [root@linux ~]# cat /etc/passwd | \ > sed -e '4d' -e '6c no six line' > /tmp/test/passwd # sed 後面若要接超過兩個以上的動作時,每個動作前面得加 -e 。
[root@dywHome2 ~]# diff /etc/passwd /tmp/test/passwd 4d3 < adm:x:3:4:adm:/var/adm:/bin/sh 6c5 < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line
[root@dywHome2 ~]# diff -q /etc/passwd /tmp/test/passwd Files /etc/passwd and /tmp/test/passwd differ
[root@dywHome2 ~]# diff -n /etc/passwd /tmp/test/passwd d4 1 d6 1 a6 1 no six line
[root@dywHome2 tmp]# diff -u old/ new/ > test.patch [root@dywHome2 tmp]# cat test.patch diff -u old/passwd new/passwd --- old/passwd 2008-04-16 13:14:50.000000000 -0400 +++ new/passwd 2008-04-16 11:15:12.000000000 -0400 @@ -1,9 +1,8 @@ root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin/sh daemon:x:2:2:daemon:/sbin:/bin/sh -adm:x:3:4:adm:/var/adm:/bin/sh lp:x:4:7:lp:/var/spool/lpd:/bin/sh -sync:x:5:0:sync:/sbin:/bin/sync +no six line shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/bin/sh
[root@linux ~]# diff /etc /tmp/test ......(前面省略)..... Only in /etc: paper.config diff /etc/passwd /tmp/test/passwd 4d3 < adm:x:3:4:adm:/var/adm:/sbin/nologin 6c5 < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line Only in /etc: passwd- ......(後面省略).....
[root@linux ~]# cmp [-bcsi] file1 file2 選項: -b :列出第一個的不同點之字元。 -c :同上。 -i SKIP1:SKIP2 : file1 與 file2 分別忽略前 SKIP1 與 SKIP2 位元。 -s :安靜模式,不顯示任何訊息。
[root@dywHome2 ~]# cmp /etc/passwd /tmp/test/passwd /etc/passwd /tmp/test/passwd differ: byte 94, line 4 # 不同點在第四行,而且位元數是在第 94 個位元。
[root@dywHome2 ~]# cmp -c /etc/passwd /tmp/test/passwd /etc/passwd /tmp/test/passwd differ: byte 94, line 4 is 141 a 154 l # 不同點在檔案 /etc/passwd 的第一個字元為 a,在檔案 /tmp/test/passwd 的第一個字元為 l。 # 字元 a 之八進位碼為 141,字元 l 之八進位碼為 154。
[root@dywHome2 ~]# printf '\x61\t\x6c\n' a l # \xNN : NN 為十六進位
[root@linux ~]# patch [OPTION]... [ORIGFILE [PATCHFILE]] 選項: -pNUM :取消 NUM 層目錄。 例如:假設檔名 /u/howard/src/blurfl/blurfl.c -p0 :代表 u/howard/src/blurfl/blurfl.c -p4 :代表 blurfl/blurfl.c -l :忽略空白之差異。 -i PATCHFILE :從 PATCHFILE 讀取補丁。 -o FILE :輸出補丁到檔案 FILE。 -r FILE :輸出錯誤到檔案 FILE。
[root@dywHome2 ~]# mkdir /tmp/old; cp /etc/passwd /tmp/old [root@dywHome2 ~]# mkdir /tmp/new; cp /tmp/test/passwd /tmp/new
[root@dywHome2 ~]# cd /tmp ; diff old/ new/ > test.patch # diff 製作檔案時,舊的檔案必須是在前面,亦即是 diff oldfile newfile。
[root@dywHome2 tmp]# cd /tmp/old [root@dywHome2 old]# patch passwd -i /tmp/test.patch patching file passwd # 選項 -i 亦可省略
[root@dywHome2 old]# patch passwd /tmp/test.patch -o passwd1 patching file passwd
[root@dywHome2 old]# patch passwd -i /tmp/test.patch patching file passwd Reversed (or previously applied) patch detected! Assume -R? [n] n Apply anyway? [n] n Skipping patch. 2 out of 2 hunks ignored -- saving rejects to file passwd.rej
[root@dywHome2 old]# patch passwd -i /tmp/test.patch patching file passwd Reversed (or previously applied) patch detected! Assume -R? [n] y
[root@dywHome2 old]# ll passwd* -rw-r--r-- 1 root root 1207 Apr 16 13:14 passwd -rw------- 1 root root 1156 Apr 16 13:10 passwd1 -rw-r--r-- 1 root root 1156 Apr 16 13:11 passwd.orig -rw-r--r-- 1 root root 149 Apr 16 13:12 passwd.rej
[root@dywHome2 old]# cd ..
[root@dywHome2 tmp]# diff -u old/ new/ > /tmp/test.patch [root@dywHome2 tmp]# cd old
[root@dywHome2 old]# patch -p1 </tmp/test.patch patching file passwd
[root@dywHome2 old]# patch -p0 </tmp/test.patch can't find file to patch at input line 4 Perhaps you used the wrong -p or --strip option? The text leading up to this was: -------------------------- |diff -u old/passwd new/passwd |--- old/passwd 2008-04-16 14:46:21.000000000 -0400 |+++ new/passwd 2008-04-16 11:15:12.000000000 -0400 -------------------------- File to patch:
patch -pnum <patchfile>
適用於目錄下多個檔案,較接近實際狀況。
This is file one line 2 line 3 there is no line 4, this is line 5 line 6
This is file two line 2 line 3 line 4 line 5 line 6 a new line 8
$ diff file1.c file2.c > diffs diffs 檔案如下: 1c1 < This is file one — > This is file two 4c4,5 < there is no line 4, this is line 5 — > line 4 > line 5 5a7 > a new line 8
$ patch file1.c diffs Hmm... Looks like a normal diff to me... Patching file file1.c using Plan A... Hunk #1 succeeded at 1. Hunk #2 succeeded at 4. Hunk #3 succeeded at 7. done $
$ patch -R file1.c diffs Hmm... Looks like a normal diff to me... Patching file file1.c using Plan A... Hunk #1 succeeded at 1. Hunk #2 succeeded at 4. Hunk #3 succeeded at 6. done $
printf '\x61\t\x6c\n'
,其中 \x61
代表意義為何?
printf '\x61\t\x6c\n'
,其中 \x6c
代表意義為何?