- 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
- How to create a symbolic link in Linux
- Types of Link in Linux
- Soft link
- Hard Link
- ln Command
- Creation of a Symbolic link to a File
- Creation of a Symbolic link to a Directory
- Removal of a Symbolic Link
- Conclusion
- About the author
- Shehroz Azam
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.
How to create a symbolic link in Linux
A symlink or symbolic link is a file type that contains the address or path of some other file or folder in a Linux Operating system; it is also known as a soft link because it points towards the file and holds the reference of some file. It is mostly used for managing and creating shortcuts of files; moreover, it is used to create different locations for primary folders and a lot more. It works like a string used for attaching the various files and folders inside the whole computer system.
This post will explore and learn how to create a symbolic link of a file or directory and how to remove a symbolic link in any Linux-based operating system. So, let’s dive into the learning process and see how many types of links exist in Linux.
Types of Link in Linux
Soft link
It is the same as a shortcut that we often see in the Windows Operating system, which is basically a pointer to some specific directory or file.
Hard Link
On the other side, hard links are also shortcuts of some specific file or folder, but the only difference between a hard link and the soft link is that the hard link cannot exist on some other partition or file system.
Well, the command-line utility that we use for creating links is known as ln. Let’s learn about it a little bit.
ln Command
ln command creates the hard link by default. However, if we want to create a soft link, we need to put a -s or –symbolic flag in front of the ln command.
The syntax for creating the symbolic link would be like as given below:
In the first argument, we have to provide the original file name.
In the second argument, we need to give it the symbolic filename.
If we do not provide the symbolic file or put a dot(.) instead of a symbolic file name, then the ln command will create the symbolic link in the same working directory.
Creation of a Symbolic link to a File
The symbolic link of the file can easily be created using the command given below:
Make sure to replace the original_file_name and symbolic_file_name with your desired filenames.
For example, to create a symbolic link of a text file, the command will go like this:
The ln command does not produce or show any success output on the terminal, so you can use the ls command if you want to verify the link creation. So, ls -l for verifying type the command given below:
The l in the output is the flag that mentions the symbolic file type, and the arrow symbol ( -> ) is pointing towards the original file from where the symbolic link is created.
Alright, now let’s see how to create a symbolic link to a Directory
Creation of a Symbolic link to a Directory
There is no difference in creating a symbolic link to a directory or a file. A symbolic link to a directory can easily be created by first providing the directory name and then providing the symbolic link.
For example, for creating the symbolic link of a directory named /folder to the ~/folder_link directory, the command for creating a symbolic link will be like this:
After running the command for creating the symbolic link, verify the creation of the link using the command given below:
You can witness in the screenshot attached above that the directory’s symbolic link is created successfully.
Now let’s move ahead and see how to remove a symbolic link in Linux.
Removal of a Symbolic Link
A symbolic link can easily be deleted or removed by either using the unlink or rm command. You have to provide the symbolic link to any of the unlink or rm commands, and the symbolic link will be removed from your Linux Operating system.
To remove the symbolic link using the unlink command, type the command given below:
To remove the symbolic link using the rm command, type the command given below;
A symbolic link will be removed using any of the two above given commands.
Conclusion
We have explored multiple ways to create a symbolic link to a file or directory in Linux and also learned to remove the symbolic link in any Linux-based Operating system. After reading this post, you will face no hurdle in creating and removing the symbolic link.
About the author
Shehroz Azam
A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.