Bash-比较2个文件列表及其md5校验和 [英] Bash - Compare 2 lists of files with their md5 check sums

查看:110
本文介绍了Bash-比较2个文件列表及其md5校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个带有md5sum检查文件的列表.即使文件相同,列表也具有不同的路径.我想检查每个文件的md5总和.我们正在讨论成千上万个文件,这就是为什么我需要脚本仅向我显示差异的原因.第一个列表是原始列表,第二个列表是文件的当前状态.我想查找与原始文件相比更改/不同的文件.为此,我想比较两个列表.每行上都有md5 sum和文件位置/名称.有谁知道如何做到这一点?如果其中一个列表中有一个额外的文件,会发生什么?!

I have 2 lists with files with their md5sum checks. The lists have different paths even that the files are the same. I want to check the md5 sums of each file. We are talking for thousands of files and that's why I need script to show me only the differences. The first list is the vanilla and the second is the current state of the files. I want to find which of the files are changed/different than the original. To do that I want to compare the 2 lists. On every line there is md5 sum and file location/name. Did anyone have an idea how to do that? And what happens if there is one extra file in one of the lists?!

第一个文件中带有校验和(vanila列表)的内容示例:

Example of content in first file with check sums (vanila list):

df7a0edcb7994581430379db56d8d53b  /home/user/vanila/file-1.php
e1af39e94239a944440ab2925393ae60  /home/user/vanila/file-2.php
ce74e43d24d9c36cd579e932ee94b152  /home/user/vanila/file-3.php
95b7d47ed7134912270f8d3059100e8c  /home/user/vanila/file-4.php

带有校验和(活动列表)的第二个文件中的内容示例:

Example of content in second file with check sums (active list):

df7a0edcb7994581430379db56d8d53b  /home/user/file-1.php
94b2a24a1fc9883246fc103f22818930  /home/user/file-1.1.php
e1af39e94239a944440ab2925393ae60  /home/user/file-2.php
ce74e43d24d9c36cd579e932ee94b152  /home/user/file-3.php
f5233ee990c50aade7c4e3ab9b4fe524  /home/user/file-4.php

预期结果:

To show me that file-4.php is with different md5 sum.
If shows that there is an extra file (file-1.1.php) it's a bonus!

推荐答案

尝试使用Awk这是用于此目的的正确工具,

An attempt using Awk which is the right tool meant for this,

awk -F"/" 'FNR==NR{filearray[$1]=$NF; next }!($1 in filearray){printf "%s has a different md5sum\n",$NF}' file2 file1
file4.php has a different md5sum

其中file2file1如下

$ cat file1
df7a0edcb7994581430379db56d8d53b  /home/user/vanila/file-1.php
e1af39e94239a944440ab2925393ae60  /home/user/vanila/file-2.php
ce74e43d24d9c36cd579e932ee94b152  /home/user/vanila/file-3.php
95b7d47ed7134912270f8d3059100e8c  /home/user/vanila/file-4.php

$ cat file2
df7a0edcb7994581430379db56d8d53b  /home/user/file-1.php
94b2a24a1fc9883246fc103f22818930  /home/user/file-1.1.php
e1af39e94239a944440ab2925393ae60  /home/user/file-2.php
ce74e43d24d9c36cd579e932ee94b152  /home/user/file-3.php
f5233ee990c50aade7c4e3ab9b4fe524  /home/user/file-4.php

要查找该文件不存在于另一个文件中,

To find the file is not present in one and not in other,

awk -F"/" 'FNR==NR{filelist[$NF]=$NF; next;}!($NF in filelist){printf "%s is an extra file",$NF}' file1 file2
file-1.1.php is an extra file

这篇关于Bash-比较2个文件列表及其md5校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆