ln command on Linux (creates links)
The ln command in Linux can be used to create symbolic or physical links.
The most commonly used options for the ln command are:
- -s Creates a symbolic link. The ln command defaults to hard links.
- -f Forces the creation of a link even if it already exists.
Symbolic links can be created between files, or between directories, even on different disks and file systems.
Hard links, on the other hand, can only be created between files from the same file system on the same physical volume (disk).
Examples:
Look at the Quintana file, it has a hard link count equal to 1:
Let’s create the physical link between the Quintana archive with the name poem:
If we look with ls -l, we’ll see that the hard link counter for these files went from 1 to 2.
Link counting is the way to tell if a file has a hard link.
In this way, it is as if the same set of bytes on the disk had two names, one called a poem and the other called Quintana. The content of the two is the same, and if we change the content of one, the other will also change, since the byte set of the file is the same.
Note that both files have the same permissions, same owners, same size, and same time.
If the Quintana file is deleted, the poem file remains, and no data is lost:
Let’s now create a symbolic link called poetry from the poem file:
Notice that the symbolic link creates a pointer to the original file. It will always have the “lrwxrwxrwx” permissions, and its size will always be small (5 bytes).
The arrow ”->” and the file type “l” indicate that the file is a symbolic link.
You can edit and change the symbolic link as if you were altering the file itself, without any problem.
If you delete the original file, the link will be broken and will stop working. Generally, the system indicates coloring the original file with a black background and the link blinks.
Symbolic links help a lot with system administration, since they allow “shortcuts” to files to be created without the direct need to make copies.
The use of a hard link allows several file names to be associated with the same file, since a hard link points to the inode of a particular file. It’s like making a copy, but without actually doubling the disk space.
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
Share