使用 shell 脚本比较 2 个文件

发布于 2024-11-17 19:43:14 字数 342 浏览 1 评论 0原文

我试图找到知道两个文件是否相同的方法,并发现了这篇文章...

在 Shell 脚本中解析 Diff 的结果

我在第一个答案中使用了代码,但我认为它不起作用,或者至少我无法让它正常工作......

我什至尝试制作一个文件的副本和比较两者(副本和原始),我仍然得到答案,就好像它们不同一样,而实际上它们不应该不同。

有人可以帮我一下,或者解释一下发生了什么吗?

非常感谢;

佩克斯

I was trying to find the way for knowing if two files are the same, and found this post...

Parsing result of Diff in Shell Script

I used the code in the first answer, but i think it's not working or at least i cant get it to work properly...

I even tried to make a copy of a file and compare both (copy and original), and i still get the answer as if they were different, when they shouldn't be.

Could someone give me a hand, or explain what's happening?

Thanks so much;

peixe

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

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

发布评论

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

评论(2

ぇ气 2024-11-24 19:43:14

您是否试图比较两个文件是否具有相同的内容,或者您​​是否试图查找它们是否是同一文件(两个硬链接)?

如果您只是比较两个文件,请尝试:

diff "$source_file" "$dest_file" # without -q

cmp "$source_file" "$dest_file" # without -s

以查看假定的差异。

您还可以尝试 md5sum

md5sum "$source_file" "$dest_file"

如果两个文件返回相同的校验和,则它们是相同的。

Are you trying to compare if two files have the same content, or are you trying to find if they are the same file (two hard links)?

If you are just comparing two files, then try:

diff "$source_file" "$dest_file" # without -q

or

cmp "$source_file" "$dest_file" # without -s

in order to see the supposed differences.

You can also try md5sum:

md5sum "$source_file" "$dest_file"

If both files return same checksum, then they are identical.

冰魂雪魄 2024-11-24 19:43:14

comm 是一个比较文件的有用工具。

comm 实用程序将读取 file1file2,这应该是
按当前整理顺序排序,并产生三个
作为输出的文本列:仅在 file1 中的行;行仅在
文件2;以及两个文件中的行。

comm is a useful tool for comparing files.

The comm utility will read file1 and file2, which should be
ordered in the current collating sequence, and produce three
text columns as output: lines only in file1; lines only in
file2; and lines in both files.

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