Copy a Directory Along With Its Relative Path

How to copy a directory from path A to path B, preserving its relative path?

For example, if the directory is found at:

./old/path/of/dir

and we want to copy it to

./new/place/

We want the result look like this:

./new/place/old/path/to/dir

The trick is to use the –parents flag of the “cp” command. So this command will do the job:

cp -r --parents old/path/to/dir new/place/.

And we’re done.