How to copy a folder structure and make symbolic links to files?
Here's the solution on non-embedded Linux and Cygwin:
cp -as SOURCE/ COPY
Note that SOURCE must be an absolute path and have a trailing slash. If you want to give a relative path, you can use
cp -as "$(pwd)/SOURCE/" COPY
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
You can try a couple of find commands like this:
mkdir FULL-PATH-TO-COPY
cd SOURCE
find . \( ! -regex '\.' \) -type d -exec mkdir FULL-PATH-TO-COPY/{} \;
find * -type f -exec ln -s `pwd`/{} FULL-PATH-TO-COPY/{} \;