How Do I Compare Two Files and Show The Differences?

本文介绍如何使用diff命令比较两个文件之间的差异,并解释了输出结果的含义。此外还提供了实用的命令选项,如忽略空格和大小写差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How Do I Compare Two Files and Show The Differences?


If, when you usefind, you discover two files with suspiciously similar names, you might want to know whether they really contain the same information. Thediffcommand will compare two files and give you the lowdown on just how different they are. Here's an example of how you would use thediffcommand and the output you might get from it:
diff cookies.old cookies.new
5c5
< One cup vanilla extract
---
> One teaspoon vanilla extract
7d6
< Six ounces chopped liver
21a22
> Note: The previous version of this recipe had a few errors!
The output is actually a description of how to transform the old file into the new one. Here,diffis telling you three things:
�The fifth line of the file has undergone a change. The5c5message says to replace line 5 of the old file with line 5 of the new file. Both the old and new text are displayed, separated by a line of three dashes. (The less-than (<) notation means "remove this line," and the greater-than (>) sign means "add this line.")
�Line 7 of the old file does not appear in the new file. The7d6message says to delete line 7 from the old file, and the files will then be in sync, starting at line 6 of the new file. The text to be deleted is displayed on the next line.
�A line was added to the new file. The21a22message says to add a new line after line 21 of the old file. The text to be added is displayed on the final line of the output.
Two useful flags you can specify when comparing files are-b(ignore blanks) and-i(ignore case). You can use them separately or in combination. The ignore blanks feature is especially useful when you're comparing the source code for two programs, since indentation changes are rarely significant. For example, here's how you might compare two program files,ur2cool.candur2cool.backup:
diff -b -i ur2cool.c ur2cool.backup
Don't worry if you have some difficulty understanding the output of thediffcommand. It's c ryptic because it was designed to be used by computers, not humans. (Some source-code control systems used by programmers usediffto keep a record of the changes made to a program over time.)
Do pay attention, though, to the less-than and greater-than indicators to see at a glance what was added or deleted; and be happy if you manage to get a general feeling for the ways the files differ.
For more information on thediffcommand, see thediff manual.

Read more:http://lowfatlinux.com/linux-compare-files-diff.html#ixzz1adswV6Kk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值