sandeep007
sandeep007

Reputation: 383

count number of files with same content in 2 different folders in shell script

I have 2 folders in my unix, named folderA and folderB with 5 files each

FolderA              FolderB
file1                file1
file2                file2
file3                file3
file4                file4
file5                file5

Assume file1,file2 contents in both the folders are same

I need output as "The 2 directories has 2 files with same content and 3 files with different content"

Also the file names in both directories are same and number of files in both directories would be same in number(no special scenarios). I did something like this

diff -U 0 /FolderA /FolderB | grep -v ^@ | wc -l

I got output as 22 and i think this is for all the differences in all the files. somehow i need to get each file differences and write for and if condition to get count. I am fairly new to Unix, so unable to figure out.

Upvotes: 2

Views: 470

Answers (2)

Nuetrino
Nuetrino

Reputation: 1119

To get the files that differ

diff -qrs dir1 dir2 | grep differ | wc -l

To get the files that are identical

diff -qrs dir1 dir2 | grep identical | wc -l

Upvotes: 1

Cyrus
Cyrus

Reputation: 88949

#!/bin/bash

f1="FolderA"
f2="FolderB"

cd "$f1" || exit 1
for i in file*; do if diff "$i" "../$f2/$i" >/dev/null; then ((same++)); else ((diff++)); fi; done

echo "$same files with same content and $diff files with different content"

Output:

2 files with same content and 3 files with different content

Upvotes: 1

Related Questions