Symbolic link or soft link is a special type of file that contains a reference, which points to another file or directory. It is supported on almost all Linux distributions. Although it points to their target, they operate independently of their targets. We can create links for both files and directories. Removing symbolic link doesn't affect anything but when the original file is removed, the link becomes a 'dangling' link that points to a nonexistent file. Unlike hard link soft links have different inode numbers, ie deleting target file or directory does not remove soft links. These links could be also created across the filesystem.
Create symbolic link for folder
ln command is used to create symbolic links and it uses the following syntax:
ln -s [target directory or file] [symlink shortcut]
For example, to link the /usr/local/lib/sample directory to /sample folder, use the following command:
# ln -s /usr/local/lib/sample /sample
After creating the symbolic link to the folder /sample and it should point to /usr/local/lib/sample, and when user changes directory (cd) to /sample, Operating system shell will automatically change to /usr/local/lib/sample directory. You can view link using ls command. You might have noticed, file type field is showing
l that indicates that it is a symbolic link and
-> shows link path.
# cd /sample # ls -l lrwxrwxrwx 1 root root 21 Apr 7 05:16 sample -> /usr/local/lib/sample
Create symbolic link for files
The symbolic link could be used for files also using the same ln command. Below, example will show how to create a new link (soft) file "filesync" which point to /home/bobbin/sync.sh
# ln -s /home/bobbin/sync.sh filesync
This will create a new link file named selfi.sh in the current directory which will point to /home/bobbin/selfi.sh
# ln -s /home/bobbin/selfi.sh
Symbolic links are used for linux programs, as aliased so that users does not have to know which version of code used.
# which python /usr/bin/python # ls -l /usr/bin/python lrwxrwxrwx. 1 root root 7 Feb 21 13:38 /usr/bin/python -> python2
Don't use soft links as an alternative for backup. Backup is two identical set of data where links are alias names for file or directory.
You might also like
Filed Under : LINUX HOWTO