There are at least 2 standard utilities to build a shadow directory tree of an existing tree, so no need to write code here.
First there's lndir(1)
from the xutils-dev
package. It uses symlinks to files. From the man page:
NAME
lndir - create a shadow directory of symbolic links to another
directory tree
SYNOPSIS
lndir [ -silent ] [ -ignorelinks ] [ -withrevinfo ] fromdir [ todir ]
A perhaps better alternative is to simply use cp
with the right options as the accepted answer suggests. I'll just give some more hopefully useful detail:
cp -al /src/dir /dest/dir # hard-links to leaf-files
cp -as /src/dir /dest/dir # symlinks to leaf-files
If you don't care about preserving all attributes (ownerships/permissions, times) replace the a
option (equivalent to -dr --preserve=all
)
with r
(recursive only):
cp -rl /src/dir /dest/dir # hard-links to leaf-files
cp -rs /src/dir /dest/dir # symlinks to leaf-files