In this part of RHCSA exam preparation we will turn our attention to links. There are two types of link, hard links and soft links. In this article we will talk about how to create and remove links and will also discuss some basic background behind both, the hard links and the soft links.
In this tutorial you will learn:
- What are symbolic (soft) links
- What are hard links
- How to create a symbolic link
- How to create a hard link
- How to remove link
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Red Hat Enterprise Linux 8 or any other GNU/Linux distribution|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
What are links on GNU/Linux systems
Every file has an information about its date of creation, modification, access as well as file ownership and its permissions stored in an so called inode. In addition to the already mentioned stored metadata, the inode also stores an information about a data block where the actual content of the file is stored on the file system.
Therefore, the main function of the inode is to describe a file-system object such as a file or a directory. In order to access the file-system object associated with a specific inode we need to provide the user with a hard link which is the actual file or directory name.
This explains the first type of links which are hard links. The second type of links on the GNU/Linux operating system are symbolic links a.k.a soft links. The difference between hard and symbolic links is that symbolic links only point to hard links, that is, they point to the existing file or directory names. In the nutshell links allow user to access files or directories via multiple names.
Since hard links are associated with the inodes which are in turn a file-system feature, the hard links cannot cross file-systems, hence are only valid within the same file system. Furthermore, it is not possible to create hard links on directories. Any attempt to create a hard link from a directory will result in
hard link not allowed for directory error message.
Let’s create some hard links. In this example we will be first creating arbitrary file called
sandbox containing a text
linuxconfig.org. Once ready we will create a multiple hard links to this file pointing from different locations.
- Create a file called
sandboxcontaining a text
RHCSAwithin a user home directory
$ echo "RHCSA" > ~/sandbox
Check the content of the file by using the
$ cat ~/sandbox RHCSA
- Still located within a user home directory create a hard link to the
$ ln sandbox /tmp/hardlink1
Now check the content of the newly created hard link
/tmp/hardlink1. The content of the
/tmp/hardlink1and the original
sandboxfile should be the same:
$ cat /tmp/hardlink1 RHCSA
- Check the link information associated with both file names
$ ls -l /tmp/hardlink1 -rw-rw-r--. 2 linuxconfig linuxconfig 6 Jul 25 10:20 /tmp/hardlink1 $ ls -l ~/sandbox -rw-rw-r--. 2 linuxconfig linuxconfig 6 Jul 25 10:20 /home/linuxconfig/sandbox
Note the associated number
2as shown by the above output. This number indicates the number of hard links associated with a specific inode.
At this stage it is important to understand that there is not real difference between the original
sandboxand the newly created
/tmp/hardlink1file. They both point the the same inode using different filenames.
- Remove hard link by using
$ unlink sandbox $ ls -l /tmp/hardlink1 -rw-rw-r--. 1 linuxconfig linuxconfig 6 Jul 25 10:20 /tmp/hardlink1
In this case, both
unlink commands will remove a hard link but not the the actual associated data and inode. Since the
sandbox hard link has been removed there is only
1 hard link left associated with the original inode. Next, we will remove the last hard link associated with this file:
$ rm /tmp/hardlink1
At this point the link to the inode pointing to the content of our original file is lost, hence we consider this file as removed. If there are no hard links pointing to an inode, the filesystem may now overwrite the this inode location with a new data.
You can remove any file ( given that you have a proper permissions) by using the unlink command? Try it now:
$ touch file $ unlink file
If you understood the above commands, then you have mastered the GNU/Linux hard links as explained in this tutorial.
In addition to hard links, there is also a different type of links available on the GNU/Linux operating system. Symbolic links can cross file-systems, and it is also possible to create a symbolic link of a directory. However, symbolic links instead of the actual inode, only link to an existing hard links (file or directory name). For this reason if the actual hard link to which the symbolic link is pointing to is removed the symbolic link become broken.
- Let’s first create some sandbox objects to play with. In this case we will be creating a directory called
mydirand within this directory we will create a file called
$ mkdir mydir $ touch mydir/myfile
- Next, we will create a new symbolic link of the existing directory mydir by using the
lncommand with a combination of
$ ln -s ~/mydir /tmp/symdir
Now, we have created a symbolic link called
symdirlocated within the
$ cd /tmp/ $ ls -l symdir lrwxrwxrwx. 1 linuxconfig linuxconfig 23 Jul 25 14:05 symdir -> /home/linuxconfig/mydir
Note the first character of the above output. In this case the character
lindicates that we are dealing with symbolic link.
When creating a symbolic links keep in mind that the
lncommand stores the actual path provided as string. If not withing the same directory, in many cases you must provide full path to order for the for the symbolic link to work
The newly created
symdirshould contain a the previously crated file
$ cd symdir $ ls myfile $pwd /tmp/symdir
- Play with symbolic links. Create a symbolic link to a file after that remove the original file and see what happened with your symbolic link.
- What happens when you execute
lscommand with only a single argument. For example execute the following command
ln -s /etc/services. What happened?
- Determine whether you need to own the file in order to create a symbolic link to it. Does the same rule applies for hard links?