- Linux ln – How to Create a Symbolic Link in Linux [Example Bash Command]
- What is the difference between soft and hard links in Linux?
- How to create a symlink to a file
- How to create a symbolic link to a directory
- How to remove a symbolic link
- How to overwrite symlinks
- How to learn more about the ln command
- Conclusion
- How to Symlink a Directory in Linux
- How to Create Symlink (soft-link) in Linux
- How to Create a Symlink (soft link) to a File
- How to Create a Symlink (Soft Link) of the Folder/Directory
- How to Overwrite the Symlink (Soft Link) in Linux:
- How to Remove Symlink (Soft Link) in Linux:
- Conclusion
- About the author
- Sam U
Linux ln – How to Create a Symbolic Link in Linux [Example Bash Command]
A symlink (symbolic) is a type of file that points to other files or directories (folders) in Linux.
You can create a symlink (symbolic) by using the ln command in the command line.
Symbolic links are useful because they act as shortcuts to a file or directory.
In this article, I will go over how to use the ln command to create a symlink to a file or directory.
What is the difference between soft and hard links in Linux?
A soft link or symbolic link will point to the original file on your system. A hard link will create a copy of the file.
Soft links can point to other files or directories on a different file system, whereas hard links cannot.
How to create a symlink to a file
You can find the command line using the Terminal application on Mac or using the Command Prompt on Windows.
Here is the basic syntax for creating a symlink to a file in your terminal.
ln -s existing_source_file optional_symbolic_link
You use the ln command to create the links for the files and the -s option to specify that this will be a symbolic link. If you omit the -s option, then a hard link will be created instead.
The existing_source_file represents the file on your computer that you want to create the symbolic link for.
The optional_symbolic_link parameter is the name of the symbolic link you want to create. If omitted, then the system will create a new link for you in the current directory you are in.
Let’s take a look at an example to better understand how this works.
On my Desktop I have a file called example_fcc_file.txt .
I will need to first open up my terminal, and then make sure I am in the Desktop directory. I can run the command cd Desktop to navigate to my Desktop.
After running that command, you should see you are now in the Desktop.
jessicawilkins@Dedrias-MacBook-Pro-2 ~ % cd Desktop jessicawilkins@Dedrias-MacBook-Pro-2 Desktop %
I can then use the ln command to create a new symbolic link called fcc_link.txt .
ln -s example_fcc_file.txt fcc_link.txt
When you run that command in the terminal, you will notice that nothing was returned. That is because when the ln command is successful, there will be no output and it will return zero.
jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % ln -s example_fcc_file.txt fcc_link.txt jessicawilkins@Dedrias-MacBook-Pro-2 Desktop %
To check that your symbolic link was successful, you can use the ls command. The ls command will list information about files and the -l flag represents the symbolic link.
When you run that command, you should see this type of result in the terminal.
lrwxr-xr-x 1 jessicawilkins staff 20 Feb 19 19:56 fcc_link.txt -> example_fcc_file.txt
The fcc_link.txt -> example_fcc_file.txt portion of the output shows you that the symbolic link is pointing to the file called example_fcc_file.txt .
You should also see that new symbolic link show up in your directory.
How to create a symbolic link to a directory
In this example, we want to create a symbolic link called my_music that will point to my Music folder in the home directory of my computer.
First, make sure you are in the home directory. You can run cd to get back to your home directory in the command line.
jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % cd jessicawilkins@Dedrias-MacBook-Pro-2 ~ %
You can then use the ln command to create a symlink to the Music directory.
ln -s /Users/jessicawilkins/Music ~/my_music
If successful, you should see it in the home directory.
How to remove a symbolic link
To remove symlink you can either use the unlink or rm command.
If we wanted to remove the fcc_link.txt symlink we created earlier, then we can use either of these commands:
Now we should see that the symlink was removed from our directory.
How to overwrite symlinks
If we try to create a new symlink called fcc_link.txt , then it will result in an error because it is already being used and pointing to another file.
ln: fcc_link.txt: File exists
You can overwrite this error by using the force ( -f ) option.
ln -sf example_fcc_file.txt fcc_link.txt
How to learn more about the ln command
If you want to learn more about the ln command, then you can read about it in the man pages (manual for using Linux commands).
Run man ln in your terminal and you should see the man pages for the ln command.
LN(1) BSD General Commands Manual LN(1) NAME link, ln -- make links SYNOPSIS ln [-Ffhinsv] source_file [target_file] ln [-Ffhinsv] source_file . target_dir link source_file target_file DESCRIPTION The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies''; instead, a link ``points'' to the original copy. There are two types of links; hard links and sym- bolic links. How a link ``points'' to a file is one of the differences between a hard and symbolic link. The options are as follows: -F If the target file already exists and is a directory, then remove it so that the link may occur. The -F option should be used with either -f or -i options. If none is specified, -f is implied. The -F option is a no-op unless -s option is specified. -h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f option, to replace a symlink which may point to a directory. -f If the target file already exists, then unlink it so that the link may occur. (The -f option overrides
Conclusion
A symlink (symbolic) is a type of file that points to other files or directories (folders) in Linux. You can create a symlink (symbolic) by using the ln command in the command line.
Symbolic links are useful because they act as shortcuts to a file or directory.
Here is the basic syntax for creating a symlink to a file using the terminal:
ln -s existing_source_file optional_symbolic_link
Here is the basic syntax for creating a symlink to a directory using the terminal:
ln -s path_to_existing_directory name_of_symbolic_link
To remove symlink you can either use the unlink or rm command:
unlink name_of_symbolic_link
If you need to remove a symlink then you can use this command:
ln -sf path_to_existing_directory name_of_symbolic_link
I hoped you enjoyed this article on symbolic links and best of luck on your programming journey.
How to Symlink a Directory in Linux
Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows. Some users refer to symlinks as soft-links. Before moving forward, let’s elaborate soft-links and hard-links.
Hard-links: Hard-links are the links that mirror or copy the original file. Hard-links have the same inode numbers.
Soft-links: Soft-links are simple links that points to the original file. You can access the original file through soft links. Soft-links can point to a file or folder in any partition and have different inode numbers.
Learning about creating symlink in Linux is a great way to improve your grip on the Linux terminal. So, let’s learn the steps involved in making the soft-links in Linux.
How to Create Symlink (soft-link) in Linux
To make symlink or soft link, we use the “ln” command. The syntax to follow to create symlink is mentioned below:
In the first argument after the “-s” option, you will be giving the path of the file of a folder you want to create the symlink of. While in the second argument, pass the name you want to give that symlink. To check the created links, use the following command:
To check inode numbers, use the command mentioned below:
How to Create a Symlink (soft link) to a File
Creating a soft link to a file is simple; use the syntax mentioned below:
Important to note that if you do not specify the “[symbolic name]”, then the command will create a symlink by the original file’s name. Let’s understand it through an example.
I have created a directory “my_folder” that contains a text file “my_doc.txt”. Now, to create symlink to “my_doc.txt” file, I will use:
As it can be seen in the above output, “my_document” is pointing to “my_folder/my_doc.txt” file. Both the symlink and the original file would have different inode number. To check inode numbers used:
Hard links will always have same inode numbers. To verify, I created a hard link of “my_doc.txt” file and name it “my_document_2”:
It can be seen in the output that the original file and the hard link have same inode numbers.
How to Create a Symlink (Soft Link) of the Folder/Directory
To create a soft-link or symlink to a directory is quite similar to creating a symlink to a file. For instance, I am creating the symlink of the “my_folder” directory using:
The above command will create a symlinked folder in the current directory. To verify it, use:
How to Overwrite the Symlink (Soft Link) in Linux:
If you try to update a symlink with the same name that already exist, then you will get an error:
We will have to use the force flag “-f” to overwrite the new path to the existing symlink.
How to Remove Symlink (Soft Link) in Linux:
In many situation, you need to remove the unnecessary symlinks from your system. To delete symlink, we use the “unlink” command, and the syntax is given below:
Let’s remove the symlinks we created in the above examples. To unlink a symlink of a file, use:
And to unlink the symlink of a directory:
We can also use the “rm” command to remove symlinks.
The advantage of “rm” over “unlink” is that you can remove multiple symlinks with the “rm” command, which is not possible with the “unlink” command as shown in the following image:
Note that whether you use the “unlink” or “rm” command, do not use trailing slash “/” even if it is a directory.
Conclusion
Symlinks are an easier way to access the files of your system from multiple locations. This write-up is a thorough guide about creating symlinks to a file or directory and removing them. Remove the symlinks if the original file no longer exists.
Understanding and mastering the Linux terminal is very crucial for any beginner. I hope this post benefitted you to learn a new utility and improve your skills.
About the author
Sam U
I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.