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? ;-)
Asked
Active
Viewed 2.3k times
25
-
1I 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? – jankes Nov 12 '11 at 23:41
-
1e.g. first synching the whole structure, but then remove some of them. There are applications for it. – kaiya Mar 15 '22 at 10:04
-
@jankes, as another example, think of a read-only filesystem you want to use as a cache, but where you might want to add additional files (to your local copy, since you can't modify the original). – Charles Duffy May 06 '22 at 19:56
2 Answers
34
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.
Pablo Castellazzi
- 1,011
- 9
- 6
-
Also my first attempt. Failed to get `cp` to create symlink for `source/some_deeper_dir/files`. – Jokester Apr 02 '13 at 16:29
-
5In 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: https://lists.gnu.org/archive/html/help-gnu-utils/2004-08/msg00039.html – 22degrees Nov 03 '16 at 21:05
8
In case cp -rs is not the answer you're looking for, lndir might be the correct answer.
Mikko Rantalainen
- 731
- 8
- 17
-
Handled perfectly deep subdirectories structures. Installed it on debian using `sudo apt-get install xutils-dev` – Guillaume Berche Sep 03 '19 at 12:29
-
-
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. – Mikko Rantalainen Mar 15 '22 at 15:08