Linux - bash script - IF condition comparing the contents of two directories (if dir1==dir2)

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

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



3.) There will be many other ways

Související obsah

linux

bash

terminal

diff

sync

Komentáře

Vaše reakce na Linux - bash script - podmínka IF obsah adresářů je stejný (if dir1==dir2)

Reference

Podívejte se na naše reference

Prohlédnout

Aplikace

Podívejte se na naše aplikace

Prohlédnout

Co umíme?

Podívejte se co umíme

Prohlédnout

Co umíme?

Vytváříme sofistikované aplikace pro náročné

Od webových aplikací přes android až po převodové můstky či složité informační systémy.

Podívejte se k nám

Máte ještě čas? Podívejte se na další rubriky

Tento web používá soubory cookie. Dalším procházením tohoto webu vyjadřujete souhlas s jejich používáním.. Více informací zde.