How to Create Symbolic Links in Linux

Create Symbolic Links

In Linux, creating symbolic links is one of the common tasks of a system administrator. This is because as a system administrator or a user, you can operate on symbolic links as if they were the actual files to which they are pointing somewhere down the line except they have been deleted. This allows you to have multiple access points to a file, without having excess copies (that remain up to date since they always access the same file). In this post, you will learn how to create a symbolic link to a file and directory, how to overwrite a symbolic link, and how to delete/remove a symbolic link.

A symbolic link also known as a symlink is a type of file in Linux that points to another file or a folder on your PC. Symbolic links are similar to shortcuts in Windows Operating System. In other words, a symbolic link is a special kind of link that helps you quickly locate another file or folder on your computer or a connected file system. This is similar to a Windows shortcut. Symbolic links can take two forms: That’s Soft Links and Hard Links.

  1. Soft links are similar to shortcuts, and can point to another file or directory in any file system. It is an indirect pointer to a file or directory. Unlike a hard link, a symbolic link can point to a file or a directory on a different filesystem or partition.
  2. Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system. You can think of a hard link as an additional name for an existing file. Hard links are associated with two or more file names with the same inode. You can create one or more hard links for a single file. Hard links cannot be created for directories and files on a different filesystem or partition.

You may also be interested in learning about the following: how to Install Terraform on Linux, how to Save Read-Only Files in VIM Editor in Linux/Unix-like OS, how to automate a process in Linux: The art of shell scripting, and how to Locate, Find, and Grep: How to search for files and patterns in Linux/Unix-like OS

Understanding the ln command

The ln is a command-line utility for creating links between files in Linux/Unix-like Operating System. The ln command syntax for creating symbolic links is as follows: By default, the ln command creates hard links. To create a symbolic link, use the -s (--symbolic) flag option.

The ln the command syntax for creating symbolic links is as follows:

ln -s {options} file link
  • If both the file and link are given, ln will create a link from the file specified as the first argument {file} to the file specified as the second argument {link}.
  • If only one file is given as an argument or the second argument is a dot {.}, ln will create a link to that file in the current working directory . The name of the symlink will be the same as the name of the file it is pointing to.

By default, on success, ln doesn’t produce any output and returns zero.

To create a symbolic link to a given file, open your terminal and type:

ln -s source_file symbolic_link

Replace source_file_name with the name of the existing file for which you want to create the symbolic link and symbolic_link_name with the name of the symbolic link. The symbolic_link _name  parameter is optional. If you do not specify the symbolic link, theln command will create a new link in your current directory:

In the following example, we are creating a symbolic link named searchtext_link.txt to a file named mysearchtext_link.txt in our terminal:

ln -s searchtext_link.txt mysearchtext_link.txt

Now, you can see from the screenshot above that we have the Symbolic Link created and highlighted, and pointing to the source file searchtext_link.txt. You can verify this by running the

To verify that the symbolic link was successfully created, let’s use the ls command to preview it:

The Output of a Symbolic Link

From the screenshot, the l character is a file-type flag that represents a symbolic link. The -> the symbol shows the file the symbolic link is pointing to. The rest of the characters after the l that’s rwxrwxrwx represent the various read, write and execute permissions in Linux usually represented as 777 or 111 in binary. To learn more bout file write and read permission, refer to the article on how to Save Read-Only Files in VIM Editor in Linux/Unix-like OS

The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symbolic link as the second parameter. For example, if you want to create a symbolic link from the /home/pictures/my_pictures directory to the ~/my_picturesthe directory you would run:

ln -s /home/pictures/my_pictures ~/my_pictures

Sometimes when you create a Symbolic with a name similar to the one you have already created, you will be prompted link already exists, the ln the command will print an error message. Let’s say we want to recreate a symlink to the same file name we have already created above that’s searchtext_link.txt to mysearchtext_link.txt

To overwrite the destination path of the symbolic link, use the -f (--force) flag option as shown below:

ln -sf searchtext_link.txt mysearchtext_link.txt

To delete/remove symbolic links use either the unlink or rm command. The command to unlink a symlink is very easy to remember. To do so use unlink symbolic link_to_remove. An example of how the command should be written in the terminal is shown below:

unlink mysearchtext_link.txt

Removing a symbolic link using the rm (remove) the command is the same as when removing a file. You can achieve this by simply running:

rm symlink_to_remove
Please note, no matter which command you us when removing a symbolic link do not append the / trailing slash at the end of its name. If you delete or move the source file to a different location, the symbolic file will be left dangling (broken) and should be removed.

In summary, to create a symbolic link in Linux use the ln command with the -s flag option. To learn more about the ln command, make use of the man command which is the command used to display the user manual of any command run on the terminal. The man command will help provide a detailed view of the ln command. To do so, type man ln in your terminal and read through the information.

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x