For this topic of the LPI LPIC-1 certification we are going to look at how we create and manage links within the Linux file-system. We have two types of links:
- Hard Links
- Soft Links (Soft links are more commonly known as Symbolic Links or simply, symlinks).
We have two videos to support his learning. The first looks at inode entries in the file-system and hard links. The second looks at symlinks. Both types of links are created with the ln command.
Hard links are files that have multiple names. A file name in Linux points to an inode, hard links are file’s inodes that have more than one file name linking to them. As such, hard links, do not take any extra disk space as there is no additional data used. File-systems also restrict the number of inodes that can be created and hard links do not create any extra data or inodes. Running ls -l or using the find command, hard links will show as regular files, however from the output of ls -l we can view in the 2nd field how many names link to the inode; if it is more than 1 there are hard links to the file.
From the output above we can see the document.txt and hello.txt are hard links, not necessarily to each other but they both contain 2 names linked to the one inode entry in the file-system as highlighted.
Hard links are restricted to the same file-system and most file-systems do not support hard linking to directories and are less commonly used than soft links. To create a hard link use the command ln:
ln <target file> <link name> ln document.txt hello.txt
The video will introduce to you inodes and what they are in the file-system and hard linking of files. Scrolling below the video we will start describing Soft Links:
Symbolics links are actual links and show as the file type link within the ls -l output and that of the command file. If directory colors are turned on then symlinks will show as light blue, but with or without colors on you will see the first character in the output of ls -l for the symlink name is “l”. This denotes a link. This is different to hard links which are regular files. From the following screenshot we can see that the file fred.txt is linked to document.txt and shows as a file type of link, denoted by the 1st character on the output of ls -l being l instead of – for regular files and hard links.
Symlinks are very similar to Aliases in the MAC OS and Shortcuts in Windows as with their counterparts in other OSs, these are completely separate files in the file-system and so have their own name, inode and data. The data of the symlink points to the original or target file. With hard links they are inodes that have more than one file name linking to them. Symlinks are more flexible in that they can link across file-systems to different partitions and drives and link to directories. To create symlinks use the command:
ln -s <target> <linkname>
ln -s /usr/share/doc ./doc
The example creates a link called doc in the current directory to the directory /usr/share/doc.