4

I have this command:

ln -sf src/* lang/golang/src/genericc/

I want to symlink all the files in src to the existing genericc directory, but when I run the above command I get broken symlinks in the destination. Anyone know how to do this?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

3 Answers3

11

Symlinks created with relative paths (i.e. where the source path doesn't start with "/") get resolved relative to the directory the link is in. That means a link to "src/foo.c" in the lang/golang/src/genericc/ directory would try to resolve to lang/golang/src/genericc/src/foo.c which probably doesn't exist.

Solution: either use an absolute path to the source files, like this:

ln -sf /path/to/src/* lang/golang/src/genericc/

or, to get the * wildcard to work right with a correct command, cd to the target directory so the relative paths will work the same way during creation that they will during resolution:

cd lang/golang/src/genericc
ln -sf ../../../../src/* ./
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
3

First of all, you can try ln -s $PATH_TO_SRC/* $PATH_TO_TARGET/. However, it might have the "Argument list too long error".

Then you can use:

find $PATH_TO_SRC/ -type f -name "*.jpg" -exec cp -s {} . \;

Because if you use ln -s with find or bash loop, it will only create an empty link. Instead, we can use cp -sto create a smylink as well.

T.Qiu
  • 31
  • 3
0

With the -r option, ln creates a link to the actual files wherever they are:

ln -srf src/* lang/golang/src/genericc/
BeniBela
  • 16,412
  • 4
  • 45
  • 52