使用 cmp 命令比较目录列表

发布于 2025-01-09 18:39:10 字数 75 浏览 3 评论 0原文

如果使用 cmp 命令比较两个目录列表(ls dir1 和 ls dir2),它会比较两个目录中的文件内容还是仅比较两个目录中的文件名?

If cmp command is used to compare two directory listings (ls dir1 and ls dir2), will it compare the contents of the files in both directories or just the file names in both directories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2025-01-16 18:39:10

您可以使用两个命令来检查文件之间的差异:cmp 使用字节比较,而 diff 使用行比较。
因此,diff 更适合文本文件,如以下示例所示:

Prompt> cat a.txt
aaa
ddd

Prompt> cat b.txt
aaa
bbb
ccc

cmp 结果:

Prompt> cmp -l a.txt b.txt
5 144 142
6 144 142
7 144 142
cmp: EOF on a.txt after byte 8

diff 结果:

Prompt> diff a.txt b.txt
2c2,3
< ddd
---
> bbb
> ccc

如您所见,它是非常清楚地看到哪些行仅存在于一个或另一个文件中。

如果您只想检查两个文件是否相等,您可以检查校验和(输出包含校验和、字节数和文件名):

Prompt> cksum a.txt
1040260371 8 a.txt
Prompt> cksum b.txt
2586209216 12 b.txt

There are two commands you can use for checking the differences between files: cmp uses byte comparison, while diff uses line comparison.
As a result, diff is better for textfiles, as you can see in following examples:

Prompt> cat a.txt
aaa
ddd

Prompt> cat b.txt
aaa
bbb
ccc

cmp result:

Prompt> cmp -l a.txt b.txt
5 144 142
6 144 142
7 144 142
cmp: EOF on a.txt after byte 8

diff result:

Prompt> diff a.txt b.txt
2c2,3
< ddd
---
> bbb
> ccc

As you see, it's very clear to see which lines only exist in one or the other file.

In case you just want to check if two files are equal or not, you might check the checksum (output contains checksum, number of bytes and filename):

Prompt> cksum a.txt
1040260371 8 a.txt
Prompt> cksum b.txt
2586209216 12 b.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文