Symbolic link source item can't be found
Asked Answered
C

3

9

I'm trying to make an alias of a directory in a set of directories

for D in $(find * -maxdepth 0 -type d) ; do
    ln -s location/to/directory/ $D/Test2 ;
done

It looks like the link is made correctly (I can see it in my finder window), but when I double click it, I get the error The operation can't be completed because the original item for "Test2" can't be found.

Why doesn't this work? Is there a way from a bash script to make a "normal" mac alias? I have opened up the permissions, as suggested here, without any luck.

Capri answered 20/4, 2015 at 21:7 Comment(0)
D
9

Use the absolute source path while creating the link. That worked for me having the same issue.

Danicadanice answered 20/5, 2018 at 11:7 Comment(0)
F
0

You want to create a symbolic link called Test2 in each directory in the current directory, and each created link should point to location/to/directory.

for dir in */; do
    ln -s 'location/to/directory' "$dir/Test2"
done

The slash after * ensures that we will only match directories in the current directory, or links to directories in the current directory.

If you're only interested in real directories an not symbolically linked directories, you may use

find . -type d -mindepth 1 -maxdepth 1 \
    -exec ln -s 'location/to/directory' {}/Test2 ';'

Note that the link destination is relative to the location of the link, so if a directory does not contain location/to/directory, the link will be "dead".

You may solve this be specifying an absolute path for the links.

Fuchsia answered 20/5, 2018 at 11:41 Comment(0)
G
-1

What are you attempting to do?

Think of a link as a cp command. Maybe that will help:

# Copies the 'svnadmin' command from /opt/svn/bin to /usr/local/bin
$ cp /opt/svn/bin/svnadmin /usr/local/bin

# Links the 'svnadmin' command from /opt/svn/bin to /usr/local/bin
$ ln -s /opt/svn/bin/svnadmin /usr/local/bin

Note that the ln and cp command have the same order of files.

In your command, you're linking whatever location/to/directory/ to $D/test2 over and over again.

Also, -maxdepth 0 won't be in the first level of the directory.

I use ln when I install new software, and the binary commands are in some other directory. Instead of building on $PATH to include all of these extra directories, I symbolically link them to /usr/local/bin:

$ cd /usr/share/apache-ant/bin
$ for file in *
> do
>     [[ -f $file ]] || continue
>     ln -s $PWD/$file /usr/local/bin/$file
> done

Note that the link simply copies the entire reference for the first file to the link. I want to make sure that this link works everywhere, so I prefix that $PWD in front of it. This way, my links look like this:

$ ls -l ant
lrwxr-xr-x  1 root  wheel  29 Sep  3  2014 ant -> /usr/share/apache-ant/bin/ant
Gabbey answered 21/4, 2015 at 3:10 Comment(3)
If the symlink B point to A, Is asking why cannot open the resource A when do a double click with finder on B.Manet
@Manet Except the Mac's Finder does open the symlink when double clicked when the link can be found. I do this with $HOME/Documents making it a symbolic link to $HOME/Dropbox/Documents. Double clicking on $HOME/Documents in the Finder works as expected. Same with the Dock or Pathfinder. All programs I use have no problems with the symbolic link. As far as the Mac is concerned, the $HOME/Documents directory is there. If the OP is having problems with the symlink, the symlink is not being made correctly. That's why I suggested putting the full path in the symlink.Gabbey
David, your comment about using the full path in the symlink did the trick for me. Thanks!Haro

© 2022 - 2024 — McMap. All rights reserved.