A symbolic link (or symlink) is a special type of file in Linux that contains a path to another file or directory. Basically, it is similar to a shortcut and is also known as a soft link.
In this post, we’ll see how to create, verify, and remove symbolic links in Linux. First, let’s start with a basic introduction to links
Hard link Vs Soft link
A hard link is a file that holds a copy of an original file. Whereas a soft link holds a reference to an original file.
|Hard link||Soft link|
|It has the same inode as the original file||It has a different inode from the original|
|It cannot be used across directories|
or file systems
|It can be used across directories or|
|Upon deletion of the original file, the|
data can still be available to the link
|Upon deletion/removal of the|
original file, the link cannot access
the contents and points to a
non-existing file (dangling link)
|Contains content of the file||Points to the file; does not contain|
any content from the original file
Here inode refers to an index node which is a data structure that holds information (attributes, disk block locations, etc.) of a file system object.
Both hard link and soft link have their own advantages and limitations. It is up to the users to choose between these two depending on their needs.
How to Create Symlinks?
To create a symbolic link is a piece of cake First checkout the syntax for creating a symlink is:
ln -s <path of the selected file/directory> <path of the symlink to be created>
ln:- It is used to create links.
-s:- a flag -s with ln command used to create symbolic links.
<pathofthefile>:- Ovehere provides the file path of the original file.
<pathofthesymlink>:– Provide the location where you want to save the symbolic link.
We will show you certain example through that you will understand How to implement symbolic links.
1. Create Symbolic link on Same directory in Linux
$ ln -s /home/ash/trendoceans.txt demo.txt
Here you create a symbolic link for the file trendoceans.txt, which is a file present in the given directory (path).
The symbolic link is named demo.txt and is present in the same directory. Any modification to the original file will be reflected on the symbolic link and vice versa.
Note:- If you try to save the symbolic file with the original file name, you will get the error “ln: failed to create symbolic link ‘trendoceans.txt’: File exists”.
2. Create Symbolic link on Different directory in Linux
$ ln -s /Desktop/ashvitha/trendoceans.txt projectwork/demo.txt
Here you create the symbolic link demo.txt to access the file trendoceans.txt on a different directory called projectwork. The directory ‘projectwork’ should pre-exist; else, the command will throw an error like “ln: failed to create symbolic link ‘projectwork/demo.txt’: No such file or directory”.
3. Create Symbolic link for folder or directory in Linux
$ ln -s /home/ashvitha ash
The above command is used to create a symbolic link to access the folder named ashvitha using ash. Again, changes to either of the links will be reflected.
There were a few examples to create symbolic or symlink, and subsequently, we will see how to verify symlink is created or not. As you know, when the symlink is generated, we not received any confirmation output.
How to Verify if Symlink is Created
If you want to verify a symlink is created or whether a file is a symlink or not then use the syntax,
ls -l <path to assumed symlink>
Note: Path is needed only if the directory is different.
$ ls -l demo.txt
lrwxrwxrwx 1 ashvitha ashvitha 15 Mar 10 19:18 projectwork/demo.txt -> trendoceans.txt
The output will contain
● small letter
‘l’ at the very beginning
‘->’ symbol followed by the original file or folder to which the symlink points to.
l is a file type flag that represents a symlink.
Remove a Symlink Created
Up till now, we have created and list out the symlink. Now, we will see how you can remove a symlink by using either of the following commands:
How to remove Symbolic link using unlink
unlink <symlink to be deleted>
$ unlink ash $ unlink demo.txt
You can have the symlink deleted upon successful execution of the process.
How to remove Symbolic link using rm
The command rm also does the same job as unlink. But both of them differ in the action.
rm <symlink to be deleted>
$ rm ash $ rm demo.txt
Note: Don’t use the ‘/’ trailing slash while specifying the symlink’s name, even for folders. Linux assumes that a directory is specified, and as directories cannot be removed or deleted, the system will throw an error.
Broken links occur when the directory of an original file is changed. In such cases, the symlinks shall become broken. You cannot access the original files using the symlinks created; an error will throw like ‘No such file or directory.
In order to find list of the broken links use syntax:
find <path of the file> -xtype l
$ find Desktop/projectwork -xtype l
find:- This command is used to search
<pathoffile>:- Over here, provide the file path
-xtype:- This flag will search only symbolic links
l:- Denote for a symbolic link.
Read this:- 20+ Find command which you can use daily
To delete the broken links:
find <path of the file> -xtype l -delete
$ find /home/ashvitha -xtype l delete
It will delete all the broken links associated with the directory ashvitha.
Symbolic links allow you to access files across the file systems very easily. It can also be used for keeping multiple copies of the same file in different directories. In short, symlinks facilitate better management of file system
That’s all to Create a Symbolic or symlink. If you are stuck somewhere, please feel free to comment down and If you like the article, or somewhere I missed something, please let me know to make this article more amazing.