Linux 如何保持两个文件夹内容一致 ?
比如
~/dock1 ~/dock2
要保证完全一致 ...
软连接是不行的好像
黄舟2017-04-17 13:27:56
I think you should do automatic backup. If it is my own code files and the like, I usually back them up manually at critical times. It is also simple, just execute cp -r. There is a problem with automatic backup. I don’t know when to back up, for example, I backed up for you in 10 minutes, but actually you want to go back to 1 hour ago, so what should I do, so I still prefer manual backup, after all, it is simple.
In addition, if you just want to compare whether two folders are completely consistent, I wrote a tool
sh
#!/usr/bin/env bash # 比较两个文件夹是否一致 # 计算每个文件文件的md5值,将所有的md5值写入一个文件中,最后再用md5比较这个文件是否一样 # mac系统下计算md5的值命令是md5,如果是linux,请换成md5sum # 用法:sh is_same.sh dir1 dir2 if [[ $# != 2 ]]; then echo "usage: sh is_same.sh <dir1> <dir2>" exit 1 fi # find查找文件,md5计算md5值,awk取出md5值,sort保证次序是一致的,将结果输出到文件中 find -type f -print | grep -v dir*.md5 | xargs md5 | awk '{print $NF}' | sort > temp.dir1.md5 find -type f -print | grep -v dir*.md5 | xargs md5 | awk '{print $NF}' | sort > temp.dir2.md5 # 比较两个文件是否相同,如果相同说明两个目录是一致的 md5sum1=$(md5 temp.dir1.md5 | awk '{print $NF}') md5sum2=$(md5 temp.dir2.md5 | awk '{print $NF}') if [[ $md5sum1 != $md5sum2 ]]; then echo " and is different" else echo " and is same" fi # 删除临时文件 rm temp.dir1.md5 rm temp.dir2.md5
PHP中文网2017-04-17 13:27:56
When both folders are local, if one is primary and the other is secondary, you should be able to use inotify + rsync
In addition, you can actually consider bit torrent sync or syncthing. Of course, this is mainly used when they are not on the same machine