Linux Link Creating

LinuxLinuxBeginner
Practice Now

Introduction

In Linux systems, links provide a powerful way to reference files and directories. These links create connections between file names and the actual data stored on the disk. Understanding how to use links effectively is an essential skill for Linux users and system administrators.

This lab will guide you through creating and using two types of links in Linux:

  1. Hard Links: These are additional directory entries that point to the same inode (data on disk). When you create a hard link, you are essentially giving the same data another name.

  2. Symbolic Links (also called soft links): These are special files that point to other files by name. Unlike hard links, symbolic links can point to directories and can span across different filesystems.

By the end of this lab, you will understand how to create both types of links using the ln command and learn their practical applications in a Linux environment.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 93% completion rate. It has received a 100% positive review rate from learners.

Creating a Working Directory

In this step, we will create a directory structure and files that we will use to practice creating links.

First, let's check our current location to ensure we're in the right directory. Run the following command:

pwd

You should see /home/labex/project as the output. If you are in a different directory, navigate to the project directory:

cd /home/labex/project

Now, let's create a new directory called linklab where we will store our files:

mkdir /home/labex/project/linklab

Let's navigate to this directory:

cd /home/labex/project/linklab

Now, let's create two text files that we will use to practice creating links:

echo "This is the original file for our link examples." > original.txt

Let's check that our file was created correctly:

ls -l

You should see output similar to the following:

-rw-r--r-- 1 labex labex 46 [date and time] original.txt

Let's also examine the content of the file:

cat original.txt

You should see the text we entered earlier displayed in the terminal:

This is the original file for our link examples.

A hard link is another name that points to the exact same data on the disk as the original file. Both the original file and the hard link share the same inode number, which means they are essentially the same file with different names.

To create a hard link, we use the ln command. Let's create a hard link to our original.txt file:

ln /home/labex/project/linklab/original.txt /home/labex/project/linklab/hardlink.txt

This command creates a new file called hardlink.txt which is a hard link to original.txt. Now, let's verify that our hard link was created correctly:

ls -li

The -i option shows the inode number for each file. You should see that original.txt and hardlink.txt have the same inode number, indicating they are the same file.

The output should look similar to the following:

[inode number] -rw-r--r-- 2 labex labex 46 [date and time] hardlink.txt
[inode number] -rw-r--r-- 2 labex labex 46 [date and time] original.txt

Notice that the number 2 after the file permissions indicates the number of hard links that point to the inode. Both original.txt and hardlink.txt show a link count of 2, because there are now two files that point to the same data.

Let's demonstrate that modifying one file affects the other, since they are essentially the same file:

echo "This is an added line." >> original.txt
cat hardlink.txt

You should see both lines displayed in the output:

This is the original file for our link examples.
This is an added line.

This confirms that changes to original.txt are reflected in hardlink.txt.

Similarly, if we modify hardlink.txt, the changes will be reflected in original.txt:

echo "Another line added through the hard link." >> hardlink.txt
cat original.txt

The output should now show all three lines:

This is the original file for our link examples.
This is an added line.
Another line added through the hard link.

Symbolic links (also known as soft links or symlinks) are different from hard links. A symbolic link is a separate file that simply points to another file by name. It does not share the same inode with the target file.

To create a symbolic link, we use the ln command with the -s option. Let's create a symbolic link to our original.txt file:

ln -s /home/labex/project/linklab/original.txt /home/labex/project/linklab/symlink.txt

This command creates a new file called symlink.txt which is a symbolic link to original.txt. Now, let's verify that our symbolic link was created correctly:

ls -li

The output should look similar to the following:

[inode number] -rw-r--r-- 2 labex labex  [size] [date and time] hardlink.txt
[inode number] -rw-r--r-- 2 labex labex  [size] [date and time] original.txt
[inode number] lrwxrwxrwx 1 labex labex  [size] [date and time] symlink.txt -> /home/labex/project/linklab/original.txt

Notice the l at the beginning of the permissions for symlink.txt, indicating it's a symbolic link. Also, the output shows the path that the symbolic link points to. You can also see that original.txt and symlink.txt have different inode numbers, confirming they are separate files.

Let's check the content of the symbolic link:

cat symlink.txt

You should see the same content as in original.txt:

This is the original file for our link examples.
This is an added line.
Another line added through the hard link.

Let's add another line through the symbolic link:

echo "This line was added through the symbolic link." >> symlink.txt
cat original.txt

The output should now include all four lines:

This is the original file for our link examples.
This is an added line.
Another line added through the hard link.
This line was added through the symbolic link.

This confirms that changes made through the symbolic link affect the target file.

Now, let's see what happens when we delete the target file:

mv original.txt original.txt.bak
cat symlink.txt

You should see an error message like:

cat: symlink.txt: No such file or directory

This is because the symbolic link still points to /home/labex/project/linklab/original.txt, which no longer exists. This is a key difference between hard links and symbolic links.

Let's restore the original file:

mv original.txt.bak original.txt
cat symlink.txt

The symbolic link works again because the target file exists once more.

Now that we have created both hard and symbolic links, let's compare their key differences:

Hard Links:

  1. Share the same inode as the original file
  2. Cannot link to directories
  3. Cannot cross file system boundaries
  4. Continue to work even if the original file is deleted or moved
  5. Changes to the content are reflected in all hard links

Symbolic Links:

  1. Have their own inode, different from the target file
  2. Can link to directories
  3. Can cross file system boundaries
  4. Become broken if the target file is deleted or moved
  5. Are essentially pointer files that contain the path to the target

Let's demonstrate some of these differences with examples:

First, let's try to create a hard link to a directory, which is not allowed:

mkdir testdir
ln testdir testdir_hardlink

You should see an error message like:

ln: testdir: hard link not allowed for directory

Now, let's try to create a symbolic link to a directory, which is allowed:

ln -s testdir testdir_symlink

Let's verify our directory symbolic link:

ls -la

You should see testdir_symlink -> testdir in the output, indicating that testdir_symlink is a symbolic link to testdir.

We can create a file inside the original directory:

echo "This is a test file in the directory." > testdir/testfile.txt

And access it through the symbolic link:

cat testdir_symlink/testfile.txt

You should see the content:

This is a test file in the directory.

This demonstrates that symbolic links can point to directories and be used to access their contents.

Another important difference is that deleting the original file breaks a symbolic link but not a hard link. We've already seen this with our symbolic link example. Let's demonstrate this with our hard link:

rm original.txt
cat hardlink.txt

You should still see all four lines:

This is the original file for our link examples.
This is an added line.
Another line added through the hard link.
This line was added through the symbolic link.

The hard link continues to work because the data still exists on disk, and the hard link still points to that data.

However, our symbolic link is now broken:

ls -l symlink.txt
cat symlink.txt

You should see that symlink.txt still exists but points to a file that no longer exists, and trying to read it produces an error.

Let's recreate the original file from our hard link:

cp hardlink.txt original.txt
cat symlink.txt

The symbolic link works again because the file it points to exists once more.

Summary

In this lab, you have learned about the two types of links in Linux: hard links and symbolic links (soft links). You have practiced creating these links using the ln command and explored their key differences.

Key points covered in this lab:

  1. Hard Links:

    • Created using the ln command without options
    • Share the same inode as the original file
    • Cannot link to directories or cross file system boundaries
    • Continue to work even if the original file is deleted
    • Changes to the content are reflected in all hard links
  2. Symbolic Links:

    • Created using the ln -s command
    • Have their own inode, different from the target file
    • Can link to directories and cross file system boundaries
    • Become broken if the target file is deleted or moved
    • Are pointer files that contain the path to the target
  3. Practical Applications:

    • Links are useful for creating shortcuts to files and directories
    • They can be used for maintaining multiple versions of files
    • System administrators use links for configuration management
    • Links help in organizing files without duplicating data

Understanding how to create and use links effectively is an essential skill for Linux users. These tools allow for efficient file management and organization within a Linux file system.