[ACCEPTED]-symlink-copying a directory hierarchy-symlink
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.
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/
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.
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.
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:
create a file shell.sh:
nano shell.sh
- 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' -
- save the file
ctrl+O
- close the file
ctrl+X
Make your script 1 executable
chmod 777 shell.sh
Run your script
./shell.sh
Happy hacking!
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.