Linux ln Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux ln command and its practical applications for creating hard links and symbolic links. The lab covers the purpose of the ln command, how to create hard links, and how to create symbolic links. You will understand the differences between these two types of links and their use cases. The lab provides step-by-step instructions and examples to help you master the ln command and its file and directory management capabilities in Linux.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/ln("`Link Creating`") subgraph Lab Skills linux/cat -.-> lab-422760{{"`Linux ln Command with Practical Examples`"}} linux/ls -.-> lab-422760{{"`Linux ln Command with Practical Examples`"}} linux/rm -.-> lab-422760{{"`Linux ln Command with Practical Examples`"}} linux/ln -.-> lab-422760{{"`Linux ln Command with Practical Examples`"}} end

Understanding the Purpose of the ln Command

In this step, you will learn about the purpose of the ln command in Linux. The ln command is used to create links, which are special files that point to other files or directories. There are two types of links: hard links and symbolic (soft) links.

Hard links are created using the ln command without any additional options. Hard links are essentially copies of the original file, but they share the same inode (file metadata) and physical storage location. This means that changes made to the file content will be reflected in all hard links.

To create a hard link, run the following command:

ln original_file hard_link_name

Example output:

$ ln file1.txt file1_hardlink.txt

Symbolic links, also known as soft links, are created using the -s option with the ln command. Symbolic links are pointers to the original file or directory, and they contain the path to the target. Unlike hard links, symbolic links can point to files or directories across file system boundaries.

To create a symbolic link, run the following command:

ln -s original_file symbolic_link_name

Example output:

$ ln -s file1.txt file1_symlink.txt

The main difference between hard links and symbolic links is that hard links are tightly coupled with the original file, while symbolic links are more flexible and can point to files or directories across the file system.

In this step, you will learn how to create hard links using the ln command.

First, let's create a sample file that we'll use for creating hard links:

touch file1.txt
echo "This is the original file." > file1.txt

Now, let's create a hard link to file1.txt using the ln command:

ln file1.txt file1_hardlink.txt

The ln command without any options creates a hard link. This means that file1_hardlink.txt and file1.txt now share the same inode (file metadata) and physical storage location. Any changes made to one file will be reflected in the other.

Let's verify that the hard link was created correctly:

ls -l file1.txt file1_hardlink.txt

Example output:

-rw-r--r-- 2 labex labex 25 Apr 12 12:34 file1.txt
-rw-r--r-- 2 labex labex 25 Apr 12 12:34 file1_hardlink.txt

Notice that both files have the same inode number (the "2" in the second column), indicating that they are hard links to the same file.

Now, let's make a change to the original file and see how it affects the hard link:

echo "This is an updated file." > file1.txt
cat file1.txt file1_hardlink.txt

Example output:

This is an updated file.
This is an updated file.

As you can see, the changes made to file1.txt are reflected in the hard link file1_hardlink.txt.

Hard links are useful when you want to create multiple references to the same file, without consuming additional storage space. However, keep in mind that hard links cannot span file system boundaries, and they cannot be created for directories.

In this step, you will learn how to create symbolic (soft) links using the ln command.

First, let's create another sample file that we'll use for creating symbolic links:

touch file2.txt
echo "This is the second file." > file2.txt

Now, let's create a symbolic link to file2.txt using the ln command with the -s option:

ln -s file2.txt file2_symlink.txt

The -s option tells the ln command to create a symbolic link instead of a hard link.

Let's verify that the symbolic link was created correctly:

ls -l file2.txt file2_symlink.txt

Example output:

-rw-r--r-- 1 labex labex 23 Apr 12 12:34 file2.txt
lrwxrwxrwx 1 labex labex  8 Apr 12 12:34 file2_symlink.txt -> file2.txt

Notice that the symbolic link file2_symlink.txt has an "l" at the beginning of the permissions, indicating that it is a symbolic link. The output also shows that file2_symlink.txt points to file2.txt.

Unlike hard links, symbolic links can point to files or directories across file system boundaries. Let's demonstrate this by creating a symbolic link to a directory:

mkdir dir1
ln -s dir1 dir1_symlink
ls -l dir1 dir1_symlink

Example output:

drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 dir1
lrwxrwxrwx 1 labex labex    5 Apr 12 12:34 dir1_symlink -> dir1

As you can see, dir1_symlink is a symbolic link that points to the dir1 directory.

Symbolic links are more flexible than hard links, as they can point to files or directories across file system boundaries. However, they can also be more fragile, as the link will break if the target file or directory is moved or deleted.

Summary

In this lab, you learned about the purpose of the ln command in Linux, which is used to create links that point to other files or directories. You explored the two types of links: hard links and symbolic (soft) links. Hard links are essentially copies of the original file that share the same inode and physical storage location, while symbolic links are pointers to the original file or directory and can cross file system boundaries. You also learned how to create hard links and symbolic links using the ln command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like