- 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
- Ln Command: How to Create Symbolic Links in Linux
- Ln Command to Create Symbolic Links
- Create a Symbolic Link to Linux Directory
- Force Overwrite Symbolic Links
- Deleting or Removing Links
- Soft Links vs Hard Links
- Soft (Symbolic) Links
- Hard Links
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.
Ln Command: How to Create Symbolic Links in Linux
A link creates a reference to a file or a folder. Symbolic links are used in Linux for managing and collating files.
In this guide, learn how to use the ln command to create symbolic links in Linux.
- A system running Linux
- Access to a terminal window / command line (Activities >Search> type Terminal)
- (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)
Ln Command to Create Symbolic Links
To use the ln command, open a terminal window and enter the command with the following format:
- By default, the ln command creates a hard link.
- Use the -s option to create a soft (symbolic) link.
- The -f option will force the command to overwrite a file that already exists.
- Source is the file or directory being linked to.
- Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.
For example, create a symbolic link with:
ln -s test_file.txt link_file.txt
This creates a symbolic link (link_file.txt) that points to the test_file.txt.
To verify whether the symlink has been created, use the ls command:
Create a Symbolic Link to Linux Directory
A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:
ln -s /mnt/external_drive/stock_photos ~/stock_photos
This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.
Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.
Force Overwrite Symbolic Links
You might receive an error message as displayed in the image below:
The error message means that there’s already a file in the destination named link_file.txt. Use the -f option to force the system to overwrite the destination link:
ln -sf test_file.txt link_file.txt
Note: Using the -f option will permanently delete the existing file.
Deleting or Removing Links
If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:
rm link_file.txt unlink link_file.txt
Soft Links vs Hard Links
The ln command can be used to create two different kinds of links:
Soft (Symbolic) Links
A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.
Here are a few important aspects of a soft link:
- If the symbolic link file is deleted, the original data remains.
- If the original file is moved or deleted, the symbolic link won’t work.
- A soft link can refer to a file on a different file system.
- Soft links are often used to quickly access a frequently-used file without typing the whole location.
Hard Links
When a file is stored on a hard drive, several things happen:
- The data is physically written to the disk.
- A reference file, calledinode, is created to point to the location of the data.
- A filename is created to refer to the inode data.
A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.
Here are a few important aspects of hard links:
- If the original file is deleted, the file data can still be accessed through other hard links.
- If the original file is moved, hard links still work.
- A hard link can only refer to a file on the same file system.
- The inode and file data are permanently deleted when the number of hard links is zero.
You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln command to create links and verify using the ls command.