A link in Linux is a shortcut to a file. Information about a file is contained in an inode, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it. A directory entry contains a name for a file or directory and a pointer to the inode where the information about the file or directory is stored. Inodes are associated with precisely one directory entry at a time. However, with hard links it is possible to associate multiple directory entries with a single inode.
Use the ln command to create a link. The '-s' switch to indicate symbolic (soft) link. Creating a link without this switch will result in a hard link.
# ln file1 file1-hard # ln -s file2 file2-soft
symbolic link (soft link) | hard link | |
Definition | A hard link points to the file by inode number. As such, hard links are no different than the first name of a file. There is no "real" name vs. hard link name; all hard links are equally valid names for the file. Because of this, the file you link to must actually exist and be in the same filesystem where you are trying to create the link. If you delete the original name, then the hard link still points to the same file. Because all hard links are equally valid name(s) for the file, you can not look at one and see the other names for the file; to find this, you have to go looking at every file and compare their inode number to find the other name(s) that have the same inode number. Hard links only allow linking on the same filesystem. | A symbolic (simlink) link or soft link points to another file by name. It has a special mode bit that identifies it as a symbolic link, and its contents are the name of the real file. Because it just contains a name, that name does not actually have to exist, or may exist on a different filesystem. If you replace the named file (change its contents without affecting its name), then the link still contains the same name, and so now it points to the new file. You can easily identify a symbolic link and see the name of the file it points to. |
Rename underlying file | rename file2 to file2-new A softlink is orphaned | rename file1 to file1-new The hard link points to new file name. |
Create two files:
# echo "file1 content" > file1 # echo "file2 content" > file2
Create hard and soft links. Use the -s switch to indicate soft link
# ln file1 file1-hard # ln -s file2 file2-soft
Now list file we just created
#ls -l -rw-r--r--. 2 root root 0 Oct 1 11:41 file1 -rw-r--r--. 2 root root 0 Oct 1 11:41 file1-hard -rw-r--r--. 1 root root 0 Oct 1 11:41 file2 lrwxrwxrwx. 1 root root 5 Oct 1 11:42 file2-soft -> file1
Changing the name of file1 does not matter. file1-hard still points to file now called file1-new. file1-hard points to the inode, the contents, of the file - that wasn't changed.
# mv file1 file1-new # cat file1-hard
Changing the name of file2 does matter. file2-soft is now an orphan link.
# mv file2 file2-new # cat file2-soft cat: file2-soft: No such file or directory
But if you create a new file2, then file2-soft will point to this new file
# echo "file2 content" > file2 # cat file2-soft file2 content