views:

191

answers:

5

What's the simplest way on Linux to "copy" a directory hierarchy so that a new hierarchy of directories are created while all "files" are just symlinks pointing back to the actual files on the source hierarchy?
'cp -s' does not work recursively.
Thanks!
-Max

+3  A: 

Starting from above the original & new directories, I think this pair of find(1) commands will do what you need:

find original -type d -exec mkdir new/{} \;
find original -type f -exec ln -s {} new/{} \;

The first instance sets up the directory structure by finding only directories in the original tree and recreating them in the new tree. The second creates the symlinks to the original files in the new tree.

Drew Stephens
+1  A: 

I googled around a little bit and found a command called lns, available from here.

JesperE
This tool works great for me. Thanks for pointing it out!
Max Spring
A: 

I just did a quick test on a linux box and cp -sR /orig /dest does exactly what you described: creates a directory hierarchy with symlinks for non-directories back to the original.

freiheit
It only works on the first directory level. For every file in subdirectories I get "xyz-file: can make relative symbolic links only in current directory".(On Ubuntu 8.10, cp version 6.10)
Max Spring
A: 

I know the question was regarding shell, but since you can call perl from shell, I wrote a tool to do something very similar to this, and posted it on perlmonks a few years ago. In my case, I generally wanted directories to remain links until I decide otherwise. It'd be a fairly trivial change to do this automatically and recursively.

Tanktalus
+1  A: 

There's also the "lndir" utility (from X) which does such a thing; I found it mentioned here: Debian Bug report #301030: can we move lndir to coreutils or debianutils? , and I'm now happily using it.

imz
I had the exact problem as the original questioner, and after starting to code a shell script, found this answer and discovered lndir already installed. Works exactly as I need it.
shmuelp