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
Manual page of the ln command
Manual page of the ln command

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Red Hat Enterprise Linux 8 or any other GNU/Linux distribution
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - 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.

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Hard links

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.

  1. Create a file called sandbox containing a text RHCSA within a user home directory ~/.
    $ echo "RHCSA" > ~/sandbox
    
    Check the content of the file by using the cat command:
    $ cat ~/sandbox
    RHCSA
    
  2. Still located within a user home directory create a hard link to the sandbox file from /tmp/ directory called hardlink1.
    $ ln sandbox /tmp/hardlink1
    
    Now check the content of the newly created hard link /tmp/hardlink1. The content of the /tmp/hardlink1 and the original sandbox file should be the same:
    $ cat /tmp/hardlink1
    RHCSA
    


  3. Check the link information associated with both file names sandbox and /tmp/hardlink1.
    $ 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 2 as shown by the above output. This number indicates the number of hard links associated with a specific inode.
    NOTE
    At this stage it is important to understand that there is not real difference between the original sandbox and the newly created /tmp/hardlink1 file. They both point the the same inode using different filenames.
  4. Remove hard link by using unlink or rm command.
  5. $ unlink sandbox 
    $ ls -l /tmp/hardlink1 
    -rw-rw-r--. 1 linuxconfig linuxconfig 6 Jul 25 10:20 /tmp/hardlink1
    
    In this case, both rm or 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.
DID YOU KNOW?
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.

Symbolic links

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.

  1. Let's first create some sandbox objects to play with. In this case we will be creating a directory called mydir and within this directory we will create a file called myfile.
    $ mkdir mydir
    $ touch mydir/myfile
    


  2. Next, we will create a new symbolic link of the existing directory mydir by using the ln command with a combination of -s option.
    $ ln -s ~/mydir /tmp/symdir
    
    Now, we have created a symbolic link called symdir located within the /tmp directory.
    $ 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 l indicates that we are dealing with symbolic link.
    NOTE
    When creating a symbolic links keep in mind that the ln command 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 symdir should contain a the previously crated file myfile:
    $ cd symdir
    $ ls
    myfile
    $pwd 
    /tmp/symdir
    

Exercises

  1. 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.
  2. What happens when you execute ls command with only a single argument. For example execute the following command ln -s /etc/services. What happened?
  3. 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?
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions