Open HenrikBengtsson opened 4 years ago
diff -rq a/ b/
(== diff --recursive --brief
) can be used to compare file content of two folders a/
and b/
. This can be used to for instance identify lost files, or modified files.
However, we have to be careful with the output, because diff -rq
will not report on missing files if a whole folder is lost - only the lost folder. Here's an example.
Let's create a backup folder and a home folder where we lost some files and folders:
mkdir -p backup/A backup/B backup/C backup/D
touch backup/file01 backup/B/file{02,03,04} backup/C/file{05,06}
cp -pR backup home
rm -rf home/B/ ## emulate lost folder
rm home/C/file05 ## emulate some lost files in folder
rm -rf home/D/ ## emulate a lost empty folder
such that we have:
$ $ tree backup
backup
├── A
├── B
│ ├── file02
│ ├── file03
│ └── file04
├── C
├── D
│ ├── file05
│ └── file06
└── file01
4 directories, 6 files
and
$ tree home
home
├── A
├── C
│ └── file06
└── file01
2 directories, 2 files
Running diff -rq
on the above gives:
$ diff -rq home/ backup/
Only in backup/: B
Only in backup/C: file05
Only in backup/: D
Note, we only get reports on B/, but not B/file02, B/file03, and B/file04, when we loose all files in a folder. Because of this, we need to do further investigation on folders B/ and D/ to know what is actually missing/different.
Test of diff -rq
with symbolic links:
mkdir -p backup/A backup/B backup/C backup/D
touch backup/file01 backup/B/file{02,03,04} backup/C/file{05,06}
ln -fs ../data2 backup/data2
ln -fs ../data1 backup/data1
cp -pR backup home
rm -rf home/B/ ## emulate lost folder
rm home/C/file05 ## emulate some lost files in folder
rm -rf home/D/ ## emulate a lost empty folder
rm backup/data2 ## emulate lost symbolic link
gives:
$ diff -rq home/ backup/
Only in backup/: B
Only in backup/C: file05
Only in backup/: D
Only in home/: data2
Result: Seems to work.
Gist: