Linux links
In Linux it is possible to create links in alternate files that are identical in content to the original file and that are updated instantly. Today I will write about those links, which are of two types:
- Symbolic links: Symbolic links are a type of file that points to another file, it can also be said that it is a new name associated with a file so it is possible for a single file to have several names, in each of them it is They will reflect the changes that the original file has.
To create symbolic links, the ln command is used together with the -s parameter as follows:
ln -s file file_link
Where file is the original file from which you want to create the symbolic link, and file_link is the name of the symbolic link. In other words, file_link points to the original file named file. To better understand what happens when we run this command, let’s do the following:
Create a file called “file” through the command “cat> file” and write inside it the phrase “Origin file”, then execute the command “ls” and you will see how the file “file” created in the directory. Now execute the following instruction: “ln -s file file_link” to create the symbolic link. To verify that the link has been created run the command “ls -l” and you will see the two files in the listing. In this case the aquamarine file will be the symbolic link, which will always have this format.
Finally to verify that the new file has the same content as the original file, run the command “cat file_link” and you will see that its content is the same.
- Hard links: A hard link is a new name associated with a file. This new file will receive all the updates that are generated in the original file, so its content will be identical.
To create hard links, use the ln command in the following way:
ln file file_link
Where file is the original file from which you want to create the link, and file_link is the name of the hard link. Next we will see how to create a hard link:
Create a file called “file” through the command “cat> file” and write the numbers from 0 to 9, then run the command “ls” and you will see how the file “file” was created in the directory. Now run the following instructions: “ln file file_link” to create the hard link. To verify that the link has been created, run the command “ls -l” and you will see both files in the list. You might notice that they are exactly the same. To check that they both have the same content run the “cat file” and “cat file_link” commands.
Differences between a symbolic link and a hard link
Symbolic and hard links have a characteristic that makes them very similar, and that is that they both update and share the same content from the original file, but at the same time they have great differences. In the previous image we can see that the file and file_link files are not the same in case 1 (symbolic link) and case 2 (hard link). In the first one we see that the permissions and dates are different, contrary to what happens with case two in which the permissions and the dates are exactly the same, this happens because they share the same inode. In addition to these differences we find the following:
1- The hard link is a file that points to the same content stored on disk as the original file. The symbolic link points to the name of a file and then the file points to content stored on the hard disk.
2- If we delete the original file we can access the content through the hard link. On the other hand, if we do it with the symbolic link, we will not have access to this content again.
3- By changing the location of the original file the hard link does not break and we can use it without any problem, the symbolic link will break if we change the location of the original file.
4- Symbolic links take up more disk space than hard links.
5- Access to content through a hard link is faster than in symbolic links, because the hard link points directly to content stored on disk, while the symbolic link points to the name of a file and then the file points to content stored on our hard drive.