Symlink multiple files to an existing folder

Alexander Mills picture Alexander Mills · Jan 5, 2019 · Viewed 8.1k times · Source

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?

Answer

Gordon Davisson picture Gordon Davisson · Jan 5, 2019

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/* ./