[ACCEPTED]-symlink-copying a directory hierarchy-symlink

Accepted answer
Score: 36

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

Score: 17
cp -as /root/absolute/path/name dest_dir

will do what you want. Note that the source 6 name must be an absolute path, it cannot 5 be relative. Else, you'll get this error: "xyz-file: can 4 make relative symbolic links only in current 3 directory."

Also, be careful as to what you're 2 copying: if dest_dir already exists, you'll 1 have to do something like:

cp -as /root/absolute/path/name/* dest_dir/
cp -as /root/absolute/path/name/.* dest_dir/
Score: 13

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

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

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

Score: 7

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

Score: 1

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

Score: 1

If you feel like getting your hands dirty Here 6 is a trick that will automatically create 5 the destination folder, subfolders and symlink 4 all files recursively.

In the folder where 3 the files you want to symlink and sub folders 2 are:

  1. create a file shell.sh:

    nano shell.sh

  2. copy and paste this charmer:

#!/bin/bash

export DESTINATION=/your/destination/folder/
export TARGET=/your/target/folder/

find . -type d -print0 | xargs -0 bash -c 'for DIR in "$@"; 
do
  echo "${DESTINATION}${DIR}"
  mkdir -p "${DESTINATION}${DIR}"        
  done' -


find . -type f -print0 |  xargs -0 bash -c 'for file in "$@"; 
do
  ln -s  "${TARGET}${file}"  "${DESTINATION}${file}"
   done' -
  1. save the file ctrl+O
  2. close the file ctrl+X
  3. Make your script 1 executable chmod 777 shell.sh

  4. Run your script ./shell.sh

Happy hacking!

Score: 0

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

More Related questions