rsync -avzh /tmp/src /tmp/dst/
How to synchronize two directories with bash code?
-
28-07-2022 - |
Question
Can any one Help me with bash code not shell command ,to get two folders sync,this what i have tried to do , I need to synchronize d1 directory with d2 with this code , but with this code all I'm getting the first file in d1 to be copied in d2 without the rest of the file ,and what would be the best way to rewrite this code in the bash file to synchronize d2 with d1,I need to solve my problem with bash code not with shell command so please,help with my code and direct me to solve my problem
#! /bin/bash
di1=$(ls d1)
n=${#di1[@]}
for (( i = 1; i <$n+1 ; i++ )); do
t=$(find d2 -name ${di1[$i]})
if [[ $t = '' ]]; then
cp d1/${di1[$i]} d2
fi
done
No correct solution
OTHER TIPS
Let me try, we need to clarify exactly what sync means:
Let's say you have two dirs
d1
L f1 10/25 11:40
L f2 10/25 10:22
L f4 10/10 08:01
d2
L f1 10/25 10:11
L f2 10/26 09:56
L f3 10/24 11:37
where d# is a directory, and f# is a file (and # means any one digit number, if you not figured it out yet), whit theirs timestamps in MM/DD hh:mm.
So you wanna have f4 copied to d2, f3 copied to d1 (no overwritten until here), then copy (and overwrite) d2/f1 with d1/f1 and d1/f2 with d2/f2.
then you should end up with:
d1
L f1 10/25 11:40
L f2 10/26 09:56
L f3 10/24 11:37
L f4 10/10 08:01
d2
L f1 10/25 11:40
L f2 10/26 09:56
L f3 10/24 11:37
L f4 10/10 08:01
Well, all I have done until now, was rephrase your question for be sure that it is in a way I can answer it, so if there is anyone reading it yet, the solution could be is as simple as:
update the existing files in the two direction
cp -ur d1 d2
cp -ur d2 d1
that's it.