Bash - Compare 2 lists of files with their md5 che

2019-07-20 04:10发布

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?!

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

Expecting results:

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!

1条回答
三岁会撩人
2楼-- · 2019-07-20 04:57

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

Where, file2 and file1 are as follows

$ 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
查看更多
登录 后发表回答