- What is Symbolic Links in Linux? How to Create Symbolic Links?
- What is Symbolic link in Linux and why is it used?
- How to create a symbolic link in Linux
- How to follow a symbolic link
- How to delete a symbolic link
- Things to keep in mind about symbolic links
- Changes made to link are reflected in the original file
- Does it link to a file or a directory? You may not know!
- You can create links to non-existent file or directory
- The symbolic links are created with 777 permission but it means nothing
- You can link to a link of a link aka chained symbolic link
- 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
What is Symbolic Links in Linux? How to Create Symbolic Links?
This detailed tutorial tells you what are symbolic links, how to create a symbolic links and other important things associated with symlinks.
What is Symbolic link in Linux and why is it used?
A symbolic link, also known as a symlink or a soft link, is a special type of file that simply points to another file or directory just like shortcuts in Windows. Creating symbolic link is like creating alias to an actual file.
If you try to access the symbolic link, you actually access the target file to which the symlink points to. Changes performed on the content of the link file change the content of the actual target file.
If you use the ls command with option -l, this is what a symbolic link looks like:
lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py
In most Linux distributions, the links are displayed in a different color than the rest of the entries so that you can distinguish the links from the regular files and directories.
Symbolic links offer a convenient way to organize and share files. They provide quick access to long and confusing directory paths. They are heavily used in linking libraries in Linux.
Now that you know a little about symbolic links, let’s see how to create them.
How to create a symbolic link in Linux
To create a symbolic link to target file from link name, you can use the ln command with -s option like this:
ln -s target_file link_name
The -s option is important here. It determines that the link is soft link. If you don’t use it, it will create a hard link. I’ll explain the difference between soft links and hard links in a different article.
How to follow a symbolic link
To know which real file the link actually points to, use the realpath command:
There are other ways to follow a soft link to its source file but realpath is the easiest.
How to delete a symbolic link
Deleting the link won’t delete the source file it links to.
You can delete multiple symbolic links in one command as well:
There is also an unlink command. But unlike the impression its name gives, the unlink command is not specifically used for deleting links. It can remove files and folders and links, of course. However, it has certain limitations that make rm command a better choice even for deleting links.
Things to keep in mind about symbolic links
Symbolic links could be confusing at times therefore you should keep note of a few things.
Changes made to link are reflected in the original file
That’s the whole purpose of the links after all. You access the target file by accessing the link. You can make changes to the target file through the links. Let’s see with example.
I have a file prog.py in newdir/test_dir. It has the following attributes:
-rw-r--r-- 1 abhishek abhishek 163 Apr 13 15:07 newdir/test_dir/prog.py
Now, I’ll create a soft link to this file in my present directory:
ln -s newdir/test_dir/prog.py link_prog
Here are the attributes of the newly created link:
lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py
Notice the l (it’s L, not one) at the beginning of the line? If you are familiar with the file permissions in Linux, you would know that the ‘l’ signifies link and thus it tells you that this file is actually a link. To refresh your memory, – means file, and d means directory.
Now if I use this link to change the content or the attributes, the same will be reflected in the target file. For example, I am using touch command on the soft link and you’ll notice that it changes the timestamp of the target file.
touch link_prog ls -l newdir/test_dir/prog.py -rw-r--r-- 1 abhishek abhishek 163 Jul 2 10:04 newdir/test_dir/prog.py
Does it link to a file or a directory? You may not know!
How would you know if the link points to file or a directory? You cannot know that until you follow the path and access the target file itself.
You can create links to non-existent file or directory
Yes, that’s totally possible. This is why you should be careful while creating soft links in Linux. The target file to which you are linking doesn’t need to exist. You won’t get any error or warning for creating link to a file/directory that does not exist.
You’ll get error only when you try to access the target file, either through the link or on its own. The ls command will still work though.
ln -s non_existant_dir link_dir less link_dir link_dir: No such file or directory
The symbolic links are created with 777 permission but it means nothing
Did you notice the file permission on the symbolic link? The symlinks are always created with 777 permission (rwxrwxrwx). For regular file, this would mean that anyone can access the file. But that’s not the case for the links.
lrwxrwxrwx 1 abhishek abhishek 23 Jul 2 08:51 link_prog -> newdir/test_dir/prog.py
If the file permissions on the links were treated as it is, any user could create a symlink to a secure file and access it freely. That would be a major security issue. Thankfully, that doesn’t happen. Because the permission on the target files matter, not the permission on links.
You may use the chmod command to change the permission on the link but it will change the permission of the linked file, not the link itself.
You can link to a link of a link aka chained symbolic link
You can make a symbolic link that points to another link and so on. This is called chained symbolic link. It’s better to avoid them as it creates more confusion.
Well, that’s it. I presume you have a better knowledge of the soft links now and you know how to create symbolic links in Linux. You may read about the symlinks command that can help you find broken symlinks in Linux and manage them easily.
If you have questions or suggestions, please leave a comment below.
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.