Diff 命令

diff是用来找不同的 :)

使用很简单:

diff [options] file1 file2

比如我又两个文件:

  1. cat diff1
  2. 1
  3. 2
  4. a
  5. cat diff2
  6. 1
  7. 2
  8. 3
  9. 4

然后我diff一下:

  1. diff diff1 diff2
  2. 3c3,4
  3. < a # < 表示在第二个文件中缺失的部分
  4. ---
  5. > 3 # > 表示在第一个文件中缺失的部分
  6. > 4

上面的结果中:

—-上面的内容是diff1文件的, —-下面的内容则是diff2文件的内容.

那么3c3,4代表的是什么?

stackexchange上面有人说可以忽略…

但是第三个答案说的就比较详细:


3d2 and 5a5 denote line numbers affected and which actions were performed. d stands for deletion, a stands for adding (and c stands for changing). the number on the left of the character is the line number in file1.txt, the number on the right is the line number in file2.txt. So 3d2 tells you that the 3rd line in file1.txt was deleted and has the line number 2 in file2.txt (or better to say that after deletion the line counter went back to line number 2). 5a5 tells you that the we started from line number 5 in file1.txt (which was actually empty after we deleted a line in previous action), added the line and this added line is the number 5 in file2.txt.


但其实知道了也没卵用… 不如vimdiff或者diff -u更直观.

扩展阅读: