paint-brush
Symbolic Links Did Not Work as Expectedby@alexandraj777
7,808 reads
7,808 reads

Symbolic Links Did Not Work as Expected

by Alexandra JohnsonAugust 15th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

If <code class="markup--code markup--p-code">source_file</code> contains a smiley face, and I want to create a <a href="https://hackernoon.com/tagged/symbolic" target="_blank">symbolic</a> link from <code class="markup--code markup--p-code">some_directory/</code> to this file, the <a href="https://kb.iu.edu/d/abbe" target="_blank">first Google result</a> for “create a symbolic link” tells me to
featured image - Symbolic Links Did Not Work as Expected
Alexandra Johnson HackerNoon profile picture

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/linkcat: 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.

Setup

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/linkcat: some_directory/link: No such file or directory

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 # :)some_directory/|-- 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

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:)

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


Copying symbolic links in Mac OS X_Join Stack Overflow to learn, share knowledge, and build your career. What is the simplest way of copying symbolic…_stackoverflow.com