Skip to content
Advertisement

Is there any way in Linux to check if directory tree has changed?

I’m creating a QEMU startup script in which I compile a rootfs to cpio.gz each time I launch the env. This is fine for the moment, but when the rootfs gets bigger in size, it is going to be a problem. Is there any way to check if a given directory structure and a compiled xxx.cpio.gz are different? I mean, if a file is added to the base rootfs directory structure, then a new cpio.gz must be created, but if its not the case, the one compiled in a previous launch would be fine.

Thanks in advance.

Advertisement

Answer

You can use diff between the rootfs dir and the cpio.gz to find files that exist only in the rootfs dir.

For example, given the following directories a and b:

a:
total 16
drwxr-xr-x  2 spiderpig spiderpig  4096 Oct  2 14:43 .
drwxr-xr-x 24 spiderpig spiderpig 12288 Oct  2 14:43 ..
-rw-r--r--  1 spiderpig spiderpig     0 Oct  2 14:43 in_both_dirs
-rw-r--r--  1 spiderpig spiderpig     0 Oct  2 14:43 only_in_a

b:
total 16
drwxr-xr-x  2 spiderpig spiderpig  4096 Oct  2 14:43 .
drwxr-xr-x 24 spiderpig spiderpig 12288 Oct  2 14:43 ..
-rw-r--r--  1 spiderpig spiderpig     0 Oct  2 14:43 in_both_dirs
-rw-r--r--  1 spiderpig spiderpig     0 Oct  2 14:43 only_in_b

the output will be:

spiderpig@linux-dev ~ $ diff --brief a b
Only in a: only_in_a
Only in b: only_in_b

Then you can know which files to copy instead of copying the whole directory. Note that diff has many options, so be sure running diff --help before using it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement