Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
    
Are you sure you don't just want to symlink bar to foo? – Rich Bradshaw Feb 11 '11 at 17:18
    
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. – Steven Xu Feb 11 '11 at 17:21
    
It seems like it would be a better idea to just configure it with --prefix=. – Hello71 Feb 12 '11 at 18:15
up vote 10 down vote accepted

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$
share|improve this answer
    
This seems to be exactly the solution. Awesome! – Steven Xu Jun 12 '11 at 16:07

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
share|improve this answer
    
does this even work? – root.ctrlc Aug 3 '15 at 18:06
    
@root.ctrlc: You have to specify the full path of the source (which is / in my original answer). I'll add a clarification. – Dennis Williamson Aug 3 '15 at 18:14
    
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. – baptx Nov 28 '15 at 18:54

Something like this?

cd /foo
for f in *; do ln -s $PWD/$f /bar; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.