Symbolic Link
Imagine create shortcut in Windows OS
If you delete the shortcut, the original file is perfectly safe.
If you move the original file to a different folder, the shortcut breaks—it still thinks the file is in the old location.
Hard Link
Imagine two people edit same documents in cloud (Google Drive, One Drive)
Everyone can see each other changes of documents immediately
However, in Linux, if one people delete the file, other people file still won't be deleted.
Symbolic Link
ln -s [Original File Directory Name] [Symbolic Link Directory & Name]
Example: ln -s log.txt symbolic_log
If you click symbolic_log, you will be direct to log.txt.
Hard Link
ln [Original File Directory Name] [Hard Link Directory & Name]
Example: ln log.txt hard_log.txt
If you edit log.txt, hard_log.txt changes.
New hard link only can be created under same filesystem in Linux.
Steps To Check Whether Two Directories Are Under Same Filesystem
df -h [Directory 1] [Directory 2]
Refer to the Filesystem column, if both directories are same means they are under same filesystem
For example: df -h /etc ~
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 22G 6.2G 16G 29% /
/dev/mapper/rhel-root 22G 6.2G 16G 29% /
ls -li [Original File / Hard Link /Symbolic Link Name 1] [Original File / Hard Link /Symbolic Link Name 2]
Example: ls -li log.txt hard_log.txt symbolic_log
Symbolic Link
Refer to the second column (file type), if start with l, means it's symbolic link
Refer to the last column (pointer) , if contain syntax like [Symbolic Link Name] --> [Original File Name], means it's symbolic link.
Hard Link
Refer to the first column (Inode number), hard link should have same inode number with original files.
Create a file for example usage
history > log.txt
Verify log.txt
ls
Create hard link hard_log.txt
ln log.txt hard_log.txt
Verify hard link
ls
ls -li log.txt hard_log.txt
hard_log.txt should have same inode number (first column) with log.txt.
Create symbolic link symbolic_log
ln -s log.txt symbolic_log
Verify symbolic link & hard link
ls
ls -li log.txt hard_log.txt symbolic_log
The start of second column (file type) of symbolic_log should start with l.
The last column (pointer) of symbolic_log should be like symbolic_log-->log.txt.
Delete original file
rm -rf log.txt
Verify symbolic link & hard link
ls
ls -li hard_log.txt symbolic_log
symbolic_log will be red text as error indicator.
Delete symbolic link & hard link
rm -rf hard_log.txt
rm -rf symbolic_log
Result
student@localhost:~$ history > log.txt
student@localhost:~$ ls
Desktop Documents Downloads log.txt Music Pictures Public Templates Videos
student@localhost:~$ ln log.txt hard_log.txt
student@localhost:~$ ls
Desktop Documents Downloads hard_log.txt log.txt Music Pictures Public Templates Videos
student@localhost:~$ ls -li log.txt hard_log.txt
37670232 -rw-r--r--. 2 student student 11091 Apr 7 09:53 hard_log.txt
37670232 -rw-r--r--. 2 student student 11091 Apr 7 09:53 log.txt
student@localhost:~$ ln -s log.txt symbolic_log
student@localhost:~$ ls
Desktop Documents Downloads hard_log.txt log.txt Music Pictures Public symbolic_log Templates Videos
student@localhost:~$ ls -li log.txt hard_log.txt symbolic_log
37670232 -rw-r--r--. 2 student student 11091 Apr 7 09:53 hard_log.txt
37670232 -rw-r--r--. 2 student student 11091 Apr 7 09:53 log.txt
37883493 lrwxrwxrwx. 1 student student 7 Apr 7 09:55 symbolic_log -> log.txt
student@localhost:~$ rm -rf log.txt
student@localhost:~$ ls
Desktop Documents Downloads hard_log.txt Music Pictures Public symbolic_log Templates Videos
student@localhost:~$ ls -li hard_log.txt symbolic_log
37670232 -rw-r--r--. 1 student student 11091 Apr 7 09:53 hard_log.txt
37883493 lrwxrwxrwx. 1 student student 7 Apr 7 09:55 symbolic_log -> log.txt
student@localhost:~$ rm -rf hard_log.txt
student@localhost:~$ rm -rf symbolic_log
student@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
student@localhost:~$