How to create a Symbolic Link on Linux / Mac

1 min read 294 words

A symbolic link – or often just sym-link – is a pointer or shortcut to where the actual file lives on a filesystem. What does it take to create a symbolic link?

The ln command actually describes it pretty well:

"Make links between files"

The manual

When should you create a symbolic link is a question I am often asked.

While creating a symbolic link can be really useful, it’s not often understood when or why we even do this.

Put simply, say you have a directory on a shared volume but need to refer to it as a local directory. Perhaps you need to store your ~/.ssh/ directory on a shared volume during a CI/CD build for example.

What is the syntax?

ln -s source_file destination_file

In our example from before, we could get the job done by using the following:

ls -s /mnt/volume_name/.ssh_shared ~/.ssh

This would make the /mnt/volume_name/.ssh_shared directory available at our user’s .ssh directory.

What the manual says about syntax:

usage: 
       ln [-Ffhinsv] source_file [target_file]
       ln [-Ffhinsv] source_file ... target_dir
       link source_file target_file

Bonus content

Symlinks are a fantastic solution if you need to have duplicate files or folders on a machine for any reason whatsoever. Using symlinks allows you to change one of the instances and have all the referenced files update automatically.

This is because there is really only one true source.

With our example from this tutorial, if we list the file with the l flag, it shows us that the file in our directory actually refers to its original source of /etc/apache2/users.

$ ls -l ~/src/apache_project/users

lrwxr-xr-x  1 ao ... ~/src/apache_project/users -> /etc/apache2/users

As we see above, it’s easy to tell if you have any symbolic links lying around.

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags