2022-06-01 14:02:40 +02:00
|
|
|
## Directories Comparison
|
|
|
|
|
|
|
|
### Objectives
|
|
|
|
|
|
|
|
1. You are given two directories as arguments and the output should be any difference between the two directories
|
|
|
|
|
2023-02-02 12:00:23 +01:00
|
|
|
### Solution 1
|
2022-06-01 14:02:40 +02:00
|
|
|
|
|
|
|
Suppose the name of the bash script is ```dirdiff.sh```
|
|
|
|
|
|
|
|
```
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
if test $# -ne 2
|
|
|
|
then
|
|
|
|
echo -e "USAGE: ./dirdiff.sh directory1 directory2"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check for the checksums.
|
|
|
|
# If both the checksums same, then both directories are same
|
|
|
|
if test `ls -1 $1 | sort | md5sum | awk -F " " '{print $1}'` == `ls -1 $2 | sort | md5sum | awk -F " " '{print $1}'`
|
|
|
|
then
|
|
|
|
echo -e "No difference between the 2 directories"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
diff -q $1 $2
|
2023-02-02 12:00:23 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Solution 2
|
2022-06-01 14:02:40 +02:00
|
|
|
|
2023-02-02 12:00:23 +01:00
|
|
|
With gnu find, you can use diff to compare directories recursively.
|
|
|
|
|
|
|
|
```shell
|
|
|
|
diff --recursive directory1 directory2
|
|
|
|
```
|