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
Share:
30,452

Related videos on Youtube

Steven
Author by

Steven

Updated on September 17, 2022

Comments

  • Steven
    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 files a, b and c, I want to create three symlinks:

    • /bar/a -> /foo/a
    • /bar/b -> /foo/b
    • /bar/c -> /foo/c
    • Rich Bradshaw
      Rich Bradshaw over 13 years
      Are you sure you don't just want to symlink bar to foo?
    • Steven
      Steven over 13 years
      The 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
      JJ_Australia over 13 years
      It seems like it would be a better idea to just configure it with --prefix=.
  • Chris
    Chris almost 9 years
    does this even work?
  • Dennis Williamson
    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
    baptx over 8 years
    I 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 like ln -s /var/www/folder/ . who duplicated subfolders files on my computer.
  • noraj
    noraj over 5 years
    This is very perfect.
  • solidau
    solidau over 4 years
    any way to make this apply for directories as well? When I run this I get warning "cp: omitting directory './baz'"
  • robsch
    robsch over 4 years
    @solidau Answer of PausedUntilFurtherNotice. does it.
  • robsch
    robsch over 4 years
    If anyone needs that: hidden files can be symlinked in a second step with ln -s /foo/.* /bar.
  • acgbox
    acgbox almost 3 years
    great best answer