Difference between a hard link and a symbolic link
In this article, and in this introduction, we will first try to learn and define what hard links and symbolic links mean. Then we will discuss what the differences are. Lets dive straight into, what the S-hell are we waiting for?
Both Linux / UNIX DO NOT allow the data of a file to have more than one name in the same place on the same file system. But both allow same if in separate places in the same file system. What that means is that two files cannot have same name in same place or directory but they can have same name but exist in separate places or directories.
Okay, lets go a bit deeper.
Symbolic links also known as Soft links are basically shortcuts to an original file or directory. When you create a symbolic link to an individual file or folder, that link will appear to be the same as the file or folder but its not; its just a link pointing to the file or folder. It can be deleted and the file or folder wont be affected in anyway because its not the actual location of the physical data.
How to create soft / symbolic links
Soft links are created with the ln command with the -s option, otherwise it would be a hard link. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
ln -s [original filename] [link name]
According to linfo.org, a hard link is merely an additional name for an existing file on Linux or other Unix-like operating systems. Any number of hard links, and thus any number of names, can be created for any file. Hard links can also be created to other hard links. However, they cannot be created for directories, and they cannot cross filesystem boundaries or span across partitions.
How to create hard links
Hard links are created with the ln command. For example, the following would create a hard link named hlink1 to a file named file1, both in the current directory (i.e., the directory in which the user is currently working).
$ ln file2 file3
From the outputs, file2 and file3 are hard linked and therefore contain the same content. Deleting file2 has no impact on file3 and vice versa. The syntax to create a hard link in Unix or Linux, at the shell prompt is as follows:
ln [original filename] [link name]
Experience the differences
1. Hard Links
- Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems.
- ls -l command shows all the links with the link column shows number of links.
- Links have actual file contents
- Removing any link, just reduces the link count, but doesn't affect other links.
- We cannot create a hard link for a directory to avoid recursive loops.
- If original file is removed then the link will still show the content of the file.
- Command to create a hard link is:
$ ln [original filename] [link name]
2. Soft Links
- A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).
- ls -l command shows all links with first column value l? and the link points to original file.
- Soft Link contains the path for original file and not the contents.
- Removing soft link doesn't affect anything but removing original file, the link becomes "dangling" link which points to nonexistent file.
- A soft link can link to a directory.
- Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.
- Command to create a Soft link is:
$ ln [original filename] [link name]
In a nut-Shell and in Linux/UNIX-like operating systems, a file with more than one name for the same data is called a hard-linked file while a file with one or more links or shortcuts to it is referred to as symbolic or soft link. Capiche?