How do I symlink each of the files in one directory to another directory?
30,452
Solution 1
You can use (GNU) cp
with the --symbolic-link
option:
prompt$ mkdir foo
prompt$ cd foo
prompt$ touch a b c
prompt$ mkdir ../bar
prompt$ cd ../bar
prompt$ cp --symbolic-link ../foo/* .
prompt$ ls -l
total 0
lrwxrwxrwx. 1 hlovdal hlovdal 8 Jun 12 16:24 a -> ../foo/a
lrwxrwxrwx. 1 hlovdal hlovdal 8 Jun 12 16:24 b -> ../foo/b
lrwxrwxrwx. 1 hlovdal hlovdal 8 Jun 12 16:24 c -> ../foo/c
prompt$
Solution 2
Give this a try:
ln -s /foo/* /bar
The source directory, as specified in the question, is /foo
. Note that it must be fully specified (i.e. starting at the root directory), so other examples would look like this:
ln -s /some/dir/with/baz/* destdir
ln -s /dir/to/link/from/* /dir/to/link/to
ln -s $PWD/stuff/* more/stuff
Solution 3
Something like this?
cd /foo
for f in *; do ln -s $PWD/$f /bar; done
Related videos on Youtube
Author by
Steven
Updated on September 17, 2022Comments
-
Steven over 1 year
If I have a directory
/foo
with a few files in it, how do I symlink each entry in/foo
into/bar/
?For instance, if
/foo
has the filesa
,b
andc
, I want to create three symlinks:/bar/a -> /foo/a
/bar/b -> /foo/b
/bar/c -> /foo/c
-
Rich Bradshaw over 13 yearsAre you sure you don't just want to symlink bar to foo?
-
Steven over 13 yearsThe actual application of this is that I installed a program and would like to move its executables to a standard folder in my $PATH rather than add the installed one to the path.
-
JJ_Australia over 13 yearsIt seems like it would be a better idea to just configure it with
--prefix=
.
-
Chris almost 9 yearsdoes this even work?
-
Dennis Williamson almost 9 years@root.ctrlc: You have to specify the full path of the source (which is
/
in my original answer). I'll add a clarification. -
baptx over 8 yearsI think if you want to copy everything as symlink with one command only, you have to use cp -s like @hlovdal answer,
cp -rs /var/www/folder/ .
copies every subfolders files as symlink, not likeln -s /var/www/folder/ .
who duplicated subfolders files on my computer. -
noraj over 5 yearsThis is very perfect.
-
solidau over 4 yearsany way to make this apply for directories as well? When I run this I get warning "cp: omitting directory './baz'"
-
robsch over 4 years@solidau Answer of PausedUntilFurtherNotice. does it.
-
robsch over 4 yearsIf anyone needs that: hidden files can be symlinked in a second step with
ln -s /foo/.* /bar
. -
acgbox almost 3 yearsgreat best answer