This article provides detailed description of Symlink and Hardlink concepts in Linux(RedHat&Centos)/Unix with meaningful examples and useful images.
**We can create more than one symbolic link for a given file/directory to use the same file/directory in different applications or different programs at a time.
**A symbolic link can have any kind of permissions as it is just a reference to the original file/dir. So, only the permissions on original file/dir matter but not of the symbolic link.
**The ownership of the softlink/symbolic link does not matter too except for the case where the link needs to be removed.
To remove a soft link, user must have valid permissions for that link.
=>This is really useful to save the some amount of disk space as we can have multiple references at different locations to one file which can sit on a single location.
**Command to create a symlink is #ln -s
Example:
Symlink to a Directory:
[root@server2 dir]# pwd
/server/dir
[root@server2 dir]# ls -lrth
total 4.0K
drwxr-xr-x 2 root root 4.0K Mar 1 11:40 test
[root@server2 dir]# cd test
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
[root@server2 test]# ln -s test test_symlink1
[root@server2 test]# ln -s test test_symlink2
[root@server2 test]# ln -s test test_symlink3
Symlink to a file:
[root@server2 test]# ln -s f1 f1_symnlink1
[root@server2 test]# ln -s f1 f1_symnlink2
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink1 -> f1
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink2 -> f1
[root@server2 test]#
What will happen if a symlink is removed?
As we already discussed, symlink or softlink or symbolic link is just a reference to the original file, absolutely there is no impact to the original file/directory with respect to their data if a symlink is removed.
How to remove a symlink?
A symlink can be removed just like a normal Linux/unix file with the below command.
#rm symlink_name
Worth Noting:
**Symlinks can be created to every file/dir to which you can locally access using the regular Linux file system commands like #cd, # cat or #ls -lrth.
**That clarifies, a symlink cannot be created to a network location unless the network file/dir is mounted on your local system.
** A symlink can also be created to a file which is non-existent. That symlink is called "Dangling Link"
We will discuss about Hard Links in my next article and then see the differences between the two.
HAPPY LINUX LEARNING :)
Feel free to add any points that can be useful to make this article complete in the comments section.
Other Related Articles:
1.File System State is clean with errors in Linux
2.SCRIPT Monitor File System Usage/size in Linux
Easy search through Google:
Use the text in quotes shown below to get into my blog quickly from Google:
"linuxunixdatabase blogger"
What is a Symlink?
A Symlink OR a soft link simply means to make a new name which is called as a symbolic link for an existing file or a directory keeping its original name and path untouched.**We can create more than one symbolic link for a given file/directory to use the same file/directory in different applications or different programs at a time.
**A symbolic link can have any kind of permissions as it is just a reference to the original file/dir. So, only the permissions on original file/dir matter but not of the symbolic link.
**The ownership of the softlink/symbolic link does not matter too except for the case where the link needs to be removed.
To remove a soft link, user must have valid permissions for that link.
Uses of a symbolic or Soft Link:
=>Symbolic links are accessible across the file systems, which means a symbolic link can be created in one file system to a file which is in another partition or file system.=>This is really useful to save the some amount of disk space as we can have multiple references at different locations to one file which can sit on a single location.
**Command to create a symlink is #ln -s
Example:
Symlink to a Directory:
[root@server2 dir]# pwd
/server/dir
[root@server2 dir]# ls -lrth
total 4.0K
drwxr-xr-x 2 root root 4.0K Mar 1 11:40 test
[root@server2 dir]# cd test
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
[root@server2 test]# ln -s test test_symlink1
[root@server2 test]# ln -s test test_symlink2
[root@server2 test]# ln -s test test_symlink3
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
If you do a cd to any of the symlinks shown above, you will find the same data which is there in test directory. That's the magic of symlink. Same is true in the below example of a file as well.
Symlink to a file:
[root@server2 test]# ln -s f1 f1_symnlink1
[root@server2 test]# ln -s f1 f1_symnlink2
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink1 -> f1
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink2 -> f1
[root@server2 test]#
What will happen if a symlink is removed?
As we already discussed, symlink or softlink or symbolic link is just a reference to the original file, absolutely there is no impact to the original file/directory with respect to their data if a symlink is removed.
How to remove a symlink?
A symlink can be removed just like a normal Linux/unix file with the below command.
#rm symlink_name
Worth Noting:
**Symlinks can be created to every file/dir to which you can locally access using the regular Linux file system commands like #cd, # cat or #ls -lrth.
**That clarifies, a symlink cannot be created to a network location unless the network file/dir is mounted on your local system.
** A symlink can also be created to a file which is non-existent. That symlink is called "Dangling Link"
We will discuss about Hard Links in my next article and then see the differences between the two.
HAPPY LINUX LEARNING :)
Feel free to add any points that can be useful to make this article complete in the comments section.
Other Related Articles:
1.File System State is clean with errors in Linux
2.SCRIPT Monitor File System Usage/size in Linux
Easy search through Google:
Use the text in quotes shown below to get into my blog quickly from Google:
"linuxunixdatabase blogger"