Create symlinks recursively for a whole tree

19,051

Solution 1

cp -rs source/ dest/ should do the trick. The directory structure will be recreated at dest/ with each file being a symlink to its counterpart in source.

Solution 2

In case cp -rs is not the answer you're looking for, lndir might be the correct answer.

Share:
19,051

Related videos on Youtube

lzap
Author by

lzap

Updated on September 18, 2022

Comments

  • lzap
    lzap over 1 year

    I am seeking for a command that would re-create a whole tree of files in a different directory. I would prefer to have all symlinks absolute. Can I do that with a find and xargs? ;-)

    • jankes
      jankes over 12 years
      I don't get the idea. If you make, for example, a symlink named /tmp/somedirectory pointing at /home/me/somegreatdir, then all the contents of somegreatdir will be visible under /tmp/somedirectory. This needs just one symlink for the entire tree. Or what else do you want?
    • kaiya
      kaiya about 2 years
      e.g. first synching the whole structure, but then remove some of them. There are applications for it.
  • Jokester
    Jokester about 11 years
    Also my first attempt. Failed to get cp to create symlink for source/some_deeper_dir/files.
  • 22degrees
    22degrees over 7 years
    In my experience, you have to use the full path to source (e.g. cp -Rs /home/myusername/source dest) otherwise it will complain. here's a ref: lists.gnu.org/archive/html/help-gnu-utils/2004-08/msg00039.h‌​tml
  • Guillaume Berche
    Guillaume Berche over 4 years
    Handled perfectly deep subdirectories structures. Installed it on debian using sudo apt-get install xutils-dev
  • kaiya
    kaiya about 2 years
    Please explain why.
  • Mikko Rantalainen
    Mikko Rantalainen about 2 years
    The lndir can handle relative links correctly whereas at least older versions of cp -rs only supported absolute paths for the first argument. The lndir is officially meant for building X Server from source code so it's actually meant to be used for one specific purpose because (1) cp -rs was not supported on all platforms and (2) cp -rs didn't support relative path as the first argument.