How to duplicate a folder exactly

Posted on Feb 22, 2023

Question

I am trying to copy a filesystem for a device I am programming for. After so much time trying to figure out why the filesystem I was installing wasn't working I found out that cp didn't get the job done. I used du -s to check the size of the original filesystem and the one that I copied with cp -r, as it turns out they differ by about 150 bytes.

Something is telling me that symbolic links or some sort of kernel objects aren't being copied correctly.

Is it possible to copy a folder/file system exactly? If so how would I go about it?

Answer

Try doing this the straightforward way :

cp -a src target

from man cp

    -a, --archive
           same as -dR --preserve=all

It preserve rights, symlinks...