
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Soft Links to Directories on Linux
A soft link (symlink or symbolic link) is generated as a shortcut to the available files or folders in the system. Usually, these soft links are used to link libraries. Soft links allow storing multiple clones of the same document in different file systems or destinations.
This link refers to the path and address of the original document. The soft link contains different aspects, such as ?
Soft links can link to both directories and regular files.
After deleting the soft link, the original document remains available.
The symbolic link does not work when you move or delete the original file.
Soft links are usually available for frequently used files, so you can access these files quickly without typing the entire location.
The inode numbers of the soft file and the actual link file are different.
A soft link functions similarly to a hyperlink on the web. If you also want to create soft links to directories on Linux, please follow this guide.
How to Create Soft Links to Directories on Linux ?
The 'ln' is a command-line utility that creates links between files or folders. By default, it creates hard links. Using the -s or --symbolic option with the ln command, you can create soft links to directories on Linux. Here is the basic syntax of ln command you can follow ?
ln -s <source file/directory name> <target directory/file name> <soft_link name> Or ln --symbolic <source directory name> <target directory/file name> <soft_link name>
For example, let's link "Documents" with the "Link" directory through the ln command ?
~$: ln -s Documents Link
If you want to verify the soft linking, go to the File Manager and check the available linked file ?
The above command doesn't produce any output after successfully creating the soft links to the directory. Hence, you can verify the newly created soft link using the following command ?
ls -l <targeted soft_link directory/file name> <soft_link name>
Let's use some examples to understand the creation of soft links better.
~$: ls -l Documents Link lrwxrwxrwx 1 prateek prateek 9 Feb 21 12:22 Link -> Documents Documents: total 24 -rw-rw-r-- 1 prateek prateek 0 Feb 20 16:10 bash.sh -rw-rw-r-- 1 prateek prateek 0 Feb 20 16:11 file.txt drwxrwxr-x 2 prateek prateek 4096 Feb 20 16:28 'Images' drwxrwxr-x 2 prateek prateek 20480 Feb 20 16:28 'Information'
Create Soft Links for the File to a Directory with the Same Name
Here we have a file 'example.txt' in the Documents directory and will link this file in the Downloads directory ?
~$:ln -s /home/prateek/Documents/example.txt Downloads
To verify the soft link, run the following command ?
~$:ls -l Downloads/example.txt lrwxrwxrwx 1 prateek prateek 35 Feb 21 13:01 Downloads/example.txt -> /home/prateek Documents example.txt
Bonus Tip ? In the above command, we have used the absolute path, which is easy and recommended. However, you have to clarify the path properly. For this, you can run the below command to make the soft link directory the current directory of the terminal ?
~$: cd ~/Downloads ~$: ln -s /home/prateek/Documents/example.txt
Creating Soft Links for The File to a Directory with Different Names
In this example, we will create a soft link to the Documents directory named Documents1 in the Downloads directory. Here, we will follow the above command and then mention the name you want to create a link with. Its command would be something like this ?
~$: ln -s /home/prateek/Documents Downloads/Documents1 ~$: ls -l Downloads/Documents1 ~$: lrwxrwxrwx 1 prateek prateek 23 Feb 21 13:21 Downloads/Documents1 -> /home/prateek Documents
This way, you can create a soft link file to a directory with a different link name.
Permissions of Soft Links
When we change the soft link's permission, it forwards to the target directory or files through the help of the chmod command. This happens because the chmod command applies permission changes to the target directory, whether the change is successful or not. Hence,
All soft links are always allowed to 0777/lrwxrwxrwx.
Soft link permission can never be changed.
Overwrite the Soft Links
The ln command prints an error message when creating a soft link that already exists (failed to create a soft link).
Using the -f or --force option with the ln command, you can overwrite the destination path of the soft link and solve this error.
ln -sf <path of the source file/directory> <path of the target file/directory>
Note ? Changes done to the original file under the updated version of Linux will automatically update or overwrite the soft link.
Find all Soft Links
You can see all the soft links present under any folder and file using the following find command ?
~$: find <path of the file/directory> -type l
For example, let's find out all the soft links of the Downloads directory ?
~$: find Downloads -type l /home/prateek/Downloads/example.txt /home/prateek/Downloads/Documents1
Find Broken Links
If you move or delete the file from one directory to another, the soft link will get broken automatically. Since the link has no content, every attempt to access that file results in 'no such file or directory.' You can again use the below find command to discover these Broken Links.
~$: find <path of the file/directory> -xtype l
As a result, this command will display all the broken soft links in the directory or file. You can delete all broken links at once by adding -the delete option to the above command.
Remove Soft Links
Soft links are also like regular files, which you can remove with the help of the rm command. Along with this, Linux also provides a separate utility called unlink to remove soft links, with the help of which you can remove the soft links.
You don't need any argument to remove soft links with the unlink command. Follow the below command to delete the soft links ?
~$: unlink <path of the soft link>
Here, we will unlink the example.txt file with the Downloads directory ?
~$: unlink /home/prateek/Documents/example.txt
Furthermore, you can also delete soft links using the rm command. The advantage of the rm command over the unlink is that you can remove multiple soft links at once ?
~$: rm <path of the file/directory> soft link1 softlink2
Conclusion
With soft links, you can access any file or directory from more than one place. Using the -s option with the ln command, you can create a soft or symbolic link in Linux. Here we described the complete procedure of creating soft links to directories. Moreover, we have explained the methods to remove, locate, and delete these links.