DEV Community

Cover image for How to make a Symbolic Link on Linux
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

How to make a Symbolic Link on Linux

Symbolic links are a link on Linux systems which point another file or folder. It means that navigating to one of these files may run a file that exists somewhere else, or it may bring you to a folder somewhere completely differently, based on how it is defined. For example, a symbolic link called /etc/link may be defined to bring you to /var/www/httpdocs.

Types of symbolic links on Linux and Mac

There are two types of symbolic links on Linux systems - hard links, and soft links.

  • Hard links are similar to another name for the same file or directory. They can only exist for files or directories on the same file system.
  • Symbolic/Soft links are like shortcuts to files or directories, rather than direct mappings to them. They can point to files or directories on different file systems.

The type of symbolic link you make can affect how other commands work on linux on those files or folders. For example, hard links can be useful if we want to easily delete the reference when using commands like rm.

For many basic uses, a symbolic or soft link works. Let's look at how to create them.

How to create symbolic links on Linux and Mac

Creating symbolic links on Linux is relatively straightforward. to do it, we use the ln command. By default, this command only makes hard links. To create a symbolic or soft link, we use the -s command. For example, the below code will make a soft link to /var/name.txt from /etc/name.txt:

ln -s /var/name.txt /etc/name.txt
Enter fullscreen mode Exit fullscreen mode

If the file /etc/name.txt/ already exists, this function will throw an error. Instead, if you still want to make the file, use the -f option to overwrite name.txt:

ln -sf /var/name.txt /etc/name.txt
Enter fullscreen mode Exit fullscreen mode

Symbolic links to directories also work in exactly the same way:

ln -sf /var /etc/fakevar
Enter fullscreen mode Exit fullscreen mode

Removing symbolic links

If you find yourself needing to remove an already created symbolic link, just use the unlink command. For example, in our above code, we linked to /var/name.txt from /etc/name.txt. To remove this symbolic link, we could write:

unlink /etc/name.txt
Enter fullscreen mode Exit fullscreen mode

This command will actually remove the symbolic link entirely - so it won't appear in your directory system anymore. So, you can also just simply remove the file using the rm command:

rm /etc/name.txt
Enter fullscreen mode Exit fullscreen mode

Conclusion

Symbolic links are useful shortcuts to other files or directories. They differ from hard links in that hard links refer directly to the same file, and share the same permissions and owners. Symbolic links on the other hand may differ from the file or folder they link to.

Latest comments (0)