简体   繁体   English

将 2 个文件与 shell 脚本进行比较

[英]Compare 2 files with shell script

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 Shell脚本中Diff的解析结果

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佩谢

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

md5sum "$source_file" "$dest_file"

If both files return same checksum, then they are identical.如果两个文件返回相同的校验和,那么它们是相同的。

comm is a useful tool for comparing files. comm是比较文件的有用工具。

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 ; comm 实用程序将读取file1file2 ,它们应按当前整理顺序排序,并生成三个文本列 output:仅在file1中的行; lines only in file2 ;仅在file2中的行; and lines in both files.和两个文件中的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM