How to Symlink a File in Linux

A symbolic link, also known as ‘Symlink‘ is a special type of file in Linux, which is used for the purpose of pointing to another file. The symlink does not contain any other data apart from the disk address of the file to which the symlink is pointing to.

Symlinks are particularly useful as shortcut files; where you can have the symlink of a program/application on your desktop/home folder, instead of the program file and its dependencies.

Another use of Symlinks is for the security of executable files. Creating a symlink and letting users execute the symlink does not grant the users any other permissions on the main file. So, there is no risk of the main program file being exploited directly or indirectly.

Today, we will learn how to create a symlink for a file in Linux.

Create a File Symlink in Linux

The command used to create links for files in Linux is ‘ln’ and it can be executed as follows:

$ ln <Name of file to be linked> <Name of the link>

For example, linking a text file:

$ ln test.txt test_link
Create File Symlink
Create File Symlink

We can see that the link has been created and it’s showing the same contents as the main file. However, when we call the command like this, it creates something called a ‘Hard link‘. A hard link points to the ‘Inode‘ of the main file and hence, it remains even if the main file is moved.

However, a symbolic link is simply a pointer to the main file name.

Create a File Symbolic Link in Linux

To create a symbolic link, run the same command, this time with the argument '-s' or '--symbolic'.

$ ln -s test.txt test_symlink
Create File Symbolic Link
Create File Symbolic Link

If you run the ‘ls -l’ command, it describes the type of file; and thus describes the symlink too.

$ ls -l
List File Symbolic Link
List File Symbolic Link

You can see that ‘test_symlink’ is pointing to test.txt, i.e., the main file which it references.

Conclusion

In this article, we have seen how to symlink a file in Linux. To learn more about the command ‘ln’, check out its man page with:

$ man ln

If you have any questions or feedback, let us know in the comments below!

Got something to say? Join the discussion.