Linux - bash script - IF condition comparing the contents of two directories (if dir1==dir2)
Hi, I need to add a condition to my bash script that moves data between two Linux machines, which will be true when two directories (local and remote) contain the same content. Because when directories are synchronized, I need to delete the data in that source directory and make sure that it is first verified that the data is actually copied to the destination directory. Thanks
Hi,
the IF condition for whether the contents of two directories are the same (if dir1 == dir2) can be done in several ways (I am not saying they are perfect, but work):
- returns different files
- dir must end with a slash
which could be applied to the IF condition as follows (using the returned rows again):
REPLY
Hi,
the IF condition for whether the contents of two directories are the same (if dir1 == dir2) can be done in several ways (I am not saying they are perfect, but work):
1.) Using diff and the number of rows returned
if [ `diff -rq dir1/ dir2/ | wc -l` != 0 ]; then
echo 'KO - dir1 != dir2'
else
echo 'OK - dir1 = dir2'
fi
2.) Use dry run on rsync
- returns different files
- dir must end with a slash
rsync -rvnc dir1/ dir2/
sending incremental file list
code-150x150.jpg
sent 2,734 bytes received 20 bytes 5,508.00 bytes/sec
total size is 4,684,691 speedup is 1,701.05 (DRY RUN)
which could be applied to the IF condition as follows (using the returned rows again):
if [ `rsync -rvnc dir1/ dir2/ | wc -l` == 4 ]; then
echo 'OK - dir1 = dir2'
else
echo 'KO - dir1 != dir2'
fi