Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a symbolic link to a directory in Ubuntu?
Symbolic links (symlinks) in Linux are advanced shortcuts that point to another file or directory on your system. A symbolic link appears to be the same as the original file or directory it references, even though it's simply a pointer. This powerful feature allows you to create references to files and directories located elsewhere in the filesystem.
What are Symbolic Links?
Symbolic links are essentially file system objects that contain a path to another file or directory. When you access a symbolic link, the operating system automatically redirects you to the target location. This is particularly useful for organizing files, creating shortcuts, or managing storage across different partitions.
Practical Example
Suppose you have a program that stores files at /home/user/Downloads/.program, but you want to store those files on another partition mounted at /external/partition. You can:
Move the
.programdirectory to/external/partition/.programCreate a symbolic link at
/home/user/Downloads/.programpointing to/external/partition/.programThe program continues working normally, but files are stored on the external partition
Creating Symbolic Links with the ln Command
The ln command is used to create both soft (symbolic) and hard links in Linux. To create symbolic links, we use the -s option.
Basic Syntax
ln -s [OPTIONS] TARGET LINK_NAME
Where:
-screates a symbolic link (soft link)TARGETis the path to the original file or directoryLINK_NAMEis the path where the symbolic link will be created
Examples
Creating a Directory Symbolic Link
ln -s /home/user/Downloads /home/user/Desktop/downloads_link
This creates a symbolic link named downloads_link on the Desktop that points to the Downloads directory.
Verification
To verify the symbolic link was created successfully, use the ls -la command:
ls -la /home/user/Desktop/downloads_link
lrwxrwxrwx 1 user user 18 May 20 11:32 downloads_link -> /home/user/Downloads
The l at the beginning indicates it's a symbolic link, and the arrow (->) shows the target directory.
Managing Symbolic Links
Removing Symbolic Links
To remove a symbolic link, use the rm command (this removes only the link, not the target):
rm /home/user/Desktop/downloads_link
Important Notes
Use absolute paths to avoid broken links when moving directories
Symbolic links can span different filesystems and partitions
If the target is deleted, the symbolic link becomes "broken" but still exists
Use
readlinkcommand to display the target of a symbolic link
Conclusion
Symbolic links are powerful tools in Linux for creating flexible file system references. They allow you to organize files efficiently, create shortcuts, and manage storage across different locations. The ln -s command makes creating directory symbolic links straightforward and is essential for system administration and file organization.
