Symlink multiple files to an existing folder
Asked Answered
R

3

5

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?

Russia answered 5/1, 2019 at 4:29 Comment(0)
C
13

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/* ./
Chlamys answered 5/1, 2019 at 5:8 Comment(0)
H
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.

Huddleston answered 10/5, 2020 at 9:4 Comment(0)
G
0

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

ln -srf src/* lang/golang/src/genericc/
Gervase answered 11/6, 2022 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.