Alexandra Johnson


Symbolic Links Did Not Work as Expected

And the easy fix from the man pages does not exist on macs

If source_file contains a smiley face, and I want to create a symbolic link from some_directory/ to this file, the first Google result for “create a symbolic link” tells me to

$ ln -s source_file some_directory/link

However, on closer inspection, it looks like something is going wrong!

$ cat some_directory/link
cat: some_directory/link: No such file or directory

Read on to experiment a little bit and see where the OS is really looking for the source of our symbolic link. If you want to skip to the end that’s fine too, I’ve included a StackOverflow link with some helpful solutions.


First, let’s setup a dummy source_file that contains an ascii smiley face

$ cd
$ echo ":)" >> source_file
$ cat source_file

Our goal is to create a symbolic link to source_file. We want to be able to $ cat some_directory/link and see the smiley face from source_file.

Like we saw above, following the the instructions on the first Google result for “create a symbolic link” doesn’t quite work how we expect

$ mkdir some_directory
$ ln -s source_file some_directory/link
$ cat some_directory/link
cat: some_directory/link: No such file or directory

Ok, Where is Our Link Pointing?

After some digging, I realized that the OS might be searching for my source file relative to the final location of my link! I tested this hypothesis by creating a new source_file adjacent to my link, and giving it a frowny face

$ echo ":'(" >> some_directory/source_file

The new directory structure looked like

source_file # :)
|-- link
|-- source_file # :(

And some_directory/link was indeed pointing to the file with the frowny face

$ cat some_directory/link
some_directory/link was not pointing to source_file, it was pointing to some_directory/source_file

Fix #1: Recursive Symbolic Links

Once I diagnosed the problem, it was easy to find the right option for ln in the man pages.

$ ln -sr source_file some_directory/other_link
$ cat some_directory/other_link

But wait! Things did not go this smoothly. As far as I can find, ln has no -r or --recursive option on mac.

Fix #2: Absolute Path

On mac, I could use the absolute path for source_file

$ ln -s ~/source_file some_directory/absolute_link
$ cat some_directory/absolute_link

Fix #3: Type Out the Relative Link

Or, another option (my preferred — not everyone checks out their git repositories into the same folder on their local dev machine) was to use a source file path relative to the symbolic link

$ ln -s ../source_file some_directory/other_link
$ cat some_directory/other_link

Conclusion aka TL/DR

As always, StackOverflow has some more great ways to fix this problem

More by Alexandra Johnson

Topics of interest

More Related Stories