If you’re a Linux user, you may have heard of hard links and symbolic links, but you might not be sure about the differences between them. One of the key differences is whether or not the link contains the data in the target file. In this article, we will explore which type of link contains the data in the target file in Linux, and explain the differences between hard links and symbolic links.
A hard link is a reference to the physical location of a file on a storage device. When a hard link is created, it creates a new file name that points directly to the same physical file on disk as the original file. Essentially, a hard link creates a second copy of the file’s inode, which is the data structure that stores information about a file on a file system.
A symbolic link, also known as a soft link, is a special type of file that serves as a pointer to another file or directory. Unlike a hard link, a symbolic link does not contain the data in the target file. Instead, it simply points to the path of the target file, which can be located anywhere in the file system.
Which type of link contains the data in the target file?
In Linux, only hard links contain the data in the target file. This means that if you create a hard link to a file, any changes made to the data in the target file will also be reflected in the hard link file because they are essentially the same physical file on disk. However, if you create a symbolic link to a file, any changes made to the data in the target file will not be reflected in the symbolic link file because they are two separate files with different inodes.
One reason to use hard links is to save disk space. Since a hard link creates a second reference to the same physical file, you can create multiple hard links to a single file without actually copying the file. This can be useful if you have a file that is used in multiple locations, but you don’t want to create multiple copies of the file.
On the other hand, symbolic links can be useful for creating shortcuts or aliases to files or directories. For example, you could create a symbolic link to a frequently used directory to make it easier to access. Additionally, symbolic links can be used to point to files on different file systems or devices.
Creating a hard link
To create a hard link, you can use the ln
command followed by the name of the target file and the name of the link. Here’s an example:
$ echo “Hello, world!” > original_file.txt
$ ln original_file.txt hard_link.txt
$ cat hard_link.txt
Hello, world!
$ echo “Goodbye, world!” >> original_file.txt
$ cat hard_link.txt
Hello, world!
Goodbye, world!
In this example, we create a file named original_file.txt
containing the text “Hello, world!”.
We then create a hard link to the file named hard_link.txt
. We can see that the contents of hard_link.txt
are the same as the contents of original_file.txt
. We then append the text “Goodbye, world!” to original_file.txt
. When we view the contents of hard_link.txt
again, we can see that the changes are reflected in the hard link file, because it’s actually the same file as original_file.txt
.
Creating a symbolic link
To create a symbolic link, you can use the ln
command with the -s
option followed by the name of the target file and the name of the link. Here’s an example:
$ ln -s original_file.txt symbolic_link.txt
$ cat symbolic_link.txt
Hello, world!
$ echo “Hello again, world!” >> original_file.txt
$ cat symbolic_link.txt
Hello, world!
In this example, we create a symbolic link named symbolic_link.txt
to original_file.txt
. We can see that the contents of symbolic_link.txt
are the same as the contents of original_file.txt
. We then append the text “Hello again, world!” to original_file.txt
. When we view the contents of symbolic_link.txt
again, we can see that the changes are not reflected in the symbolic link file, because it’s simply pointing to the path of the target file and not actually containing the data.
Note that in both cases, the files are located in the same directory. This is because hard links and symbolic links cannot point to files located in different directories or file systems.
In summary, hard links contain the data in the target file, while symbolic links simply point to the path of the target file. Hard links can be used to save disk space by creating multiple references to the same physical file, while symbolic links can be used to create shortcuts or point to files on different file systems. Understanding the differences between hard links and symbolic links can help you use them effectively in your Linux system.