Working with file links

In this section, we take a look at what Linux file links are and how to work with them. As you already might know, files are stored on the hard disk. In a Linux filesystem, the file's filename and the data are two separate concepts and are not stored together. A general structure is shown in the following diagram:

Connecting a filename to the actual data is managed by the filesystem using a table or database data structure, which is called a title allocation table. In the Linux filesystem, an Inode is the actual entry point or starting point to the beginning of a specific file's data on the hard disk. To simplify, we can just say that the Inode represents the actual data of a file. The filesystem management now takes care that every normal file, upon creation, has one link entry in its allocation table to connect the actual filename to the Inode or data on the hard disk. Such a link is also called hard link. The original filename to Inode relationship is also linked using a hard link, that's why in the last section the ls -l command gave us the number 1 for most of the files in the column adjacent to the permissions. Now, the cool thing about the Linux filesystem is that you can create additional hard links to an existing Inode, which is like having alternative names for a file.

One of the drawbacks of a hard link are that you cannot differentiate a hard link from the original filename or the Inode. This can cause problems and side effects, because if you change the original file's content the hard link's content will be changed as well. Another limitation of hard links is that you can only define them for Inodes, which are on the same partition as the hard link should go. Also, you cannot create hard links on directories. You can only create them on normal files. To solve these limitations of hard links, you can use soft links, also known as symbolic links. These are the type of links that you will use almost all the time in your everyday work as a Linux system administrator. Hard links also have their special use cases, for example, for creating backups of files, but are only used very rarely by the Linux user.

A symbolic link is a link to the filename and not to the Inode. Symbolic links also don't have the boundary that they must be on the same partition or hard disk as the original file. You can also create symbolic links on a directory. The main drawback is that if you delete or move the original file, you will have a broken symbolic link without further warning, which can also create some bad side effects. The main use cases and power of symbolic links are referencing configuration files or dynamic library versions in the Linux filesystem. Using links can save a lot of disk space because no actual data must be copied and they are very effective for quickly testing out such things as alternate configuration files for services.

File links are managed by the ln command. The basic syntax is ln [OPTION], then the filename you want to create a link on, and finally the link name. To create a hard link to a file called fileX in your home directory, use the following code:

As you can see, there's no way to differentiate the additional hard link from the original one. You can also create multiple links on the same file. To delete a hard link, use the rm command. There's a maximum number of Inodes on every filesystem, or we can just simply say files, which you can display using df -i. If you use the mount command, you will see that the tmp filesystem for the user is on a different partition than the home directory, which is in turn, on the root partition as shown in the following screenshot:

So the next command ln ~/folderABC ~/folderABC_link will fail because it is not allowed to create hard links between partitions. Also, you cannot create a hard link on a directory, and changing the origin of the file's content will change the hard link's file content as well. This can create some bad side effects. To create a symbolic link, use the ln -s option:

As you can see, it's easy to show if a file as a symbolic link marked with the arrow. To create a symbolic link of a file in another directory, preserving the original file's name, you can use ln -s /etc/passwd. This created a symbolic link of the /etc/passwd file in the current directory under the same name, passwd. To delete a symbolic link, use the rm command; the original file will not be touched. You can also create a symbolic link on a directory. If you delete the original file that the symbolic link is pointing to, that is fileX here the symbolic link will broke. This can get problematic, which is denoted here with the blue color:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.3.208