Linux symlinks Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to create and manage symbolic links, also known as symlinks, in Linux. Symbolic links are a special type of file that acts as a reference to another file or directory, allowing you to access the target file or directory through the symlink. You will understand the difference between symbolic and hard links, and explore practical use cases of symbolic links.

The lab covers the following steps: creating and managing symbolic links, understanding the difference between symbolic and hard links, and exploring practical use cases of symbolic links. You will learn how to create a symbolic link, access the target file through the symlink, and observe the behavior of the symlink when the target file is modified or deleted.

Linux Commands Cheat Sheet


Skills Graph

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

In this step, you will learn how to create and manage symbolic links, also known as symlinks, in Linux. Symbolic links are a special type of file that acts as a reference to another file or directory, allowing you to access the target file or directory through the symlink.

First, let's create a directory and a file to work with:

mkdir ~/project/source
touch ~/project/source/file.txt

Now, let's create a symbolic link to the file.txt file:

ln -s ~/project/source/file.txt ~/project/symlink.txt

Example output:

labex@ubuntu:~/project$ ls -l
total 0
lrwxrwxrwx 1 labex labex 22 May 24 12:34 symlink.txt -> /home/labex/project/source/file.txt
-rw-r--r-- 1 labex labex  0 May 24 12:34 source/file.txt

The -s option in the ln command creates a symbolic link. The first argument is the target file, and the second argument is the name of the symlink.

You can now access the file.txt file through the symlink.txt symlink:

cat ~/project/symlink.txt

Example output:

labex@ubuntu:~/project$ cat ~/project/symlink.txt

As you can see, the cat command works the same way as if you were accessing the file.txt file directly.

Let's try modifying the target file and see how the symlink behaves:

echo "Hello, World!" >> ~/project/source/file.txt
cat ~/project/symlink.txt

Example output:

labex@ubuntu:~/project$ echo "Hello, World!" >> ~/project/source/file.txt
labex@ubuntu:~/project$ cat ~/project/symlink.txt
Hello, World!

The content of the symlink reflects the changes made to the target file.

Now, let's delete the target file and see what happens to the symlink:

rm ~/project/source/file.txt
ls -l ~/project/symlink.txt

Example output:

labex@ubuntu:~/project$ rm ~/project/source/file.txt
labex@ubuntu:~/project$ ls -l ~/project/symlink.txt
lrwxrwxrwx 1 labex labex 22 May 24 12:34 /home/labex/project/symlink.txt -> /home/labex/project/source/file.txt

The symlink still exists, but it now points to a non-existent file. Trying to access the symlink will result in an error.

In summary, you have learned how to create and manage symbolic links in Linux, and how they behave when the target file is modified or deleted.

In this step, you will learn the key differences between symbolic (soft) links and hard links in Linux.

First, let's create a hard link to the file.txt file:

ln ~/project/source/file.txt ~/project/hardlink.txt

Example output:

labex@ubuntu:~/project$ ls -l
total 4
-rw-r--r-- 2 labex labex 14 May 24 12:34 file.txt
lrwxrwxrwx 1 labex labex 22 May 24 12:34 symlink.txt -> /home/labex/project/source/file.txt
-rw-r--r-- 2 labex labex 14 May 24 12:34 hardlink.txt

As you can see, the hard link hardlink.txt has the same inode number as the original file.txt file, indicating that they both refer to the same physical file on the disk.

Now, let's compare the behavior of symbolic and hard links:

  1. Target file deletion:

    • If you delete the original file.txt file, the symbolic link symlink.txt will become a "dangling" link, pointing to a non-existent file.
    • If you delete the original file.txt file, the hard link hardlink.txt will still work, as it refers to the same physical file.
  2. Disk space usage:

    • Symbolic links do not consume additional disk space, as they only contain the path to the target file.
    • Hard links share the same physical file on the disk, so they do not consume additional disk space.
  3. Cross-device linking:

    • Symbolic links can point to files or directories across different file systems or devices.
    • Hard links can only be created within the same file system, as they reference the same physical file.
  4. File type:

    • Symbolic links are a special type of file, identified by the l file type in the ls -l output.
    • Hard links are regular files, and they are indistinguishable from the original file in the ls -l output.

In summary, the key differences are that symbolic links are a reference to the target file, while hard links are an alternative name for the same physical file on the disk. Symbolic links are more flexible but can become dangling, while hard links are more robust but limited to the same file system.

In this final step, you will explore some practical use cases for symbolic links in Linux.

1. Linking Configuration Files
Symbolic links can be used to manage configuration files more efficiently. For example, you can create a symlink from /etc/nginx/sites-available/my-site.conf to /etc/nginx/sites-enabled/my-site.conf to enable a new Nginx site configuration without having to copy the file.

sudo ln -s /etc/nginx/sites-available/my-site.conf /etc/nginx/sites-enabled/my-site.conf

2. Linking Directories
Symbolic links can be used to create shortcuts to directories, making it easier to access frequently used locations. For example, you can create a symlink from ~/documents to ~/project/important-files.

ln -s ~/project/important-files ~/documents

3. Linking Executables
Symbolic links can be used to create shortcuts to executable files, making them accessible from different locations in the file system. This is particularly useful when you want to run a command from any directory without having to specify the full path.

sudo ln -s /usr/local/bin/my-script.sh /usr/bin/my-script

Now, you can run my-script from any directory on the system.

4. Linking Libraries
Symbolic links can be used to manage shared libraries more effectively. For example, you can create a symlink from a specific library version to a generic name, allowing your applications to use the latest version without modifying the code.

sudo ln -s /usr/lib/libmylib.so.1.2.3 /usr/lib/libmylib.so

These are just a few examples of how symbolic links can be used in practical scenarios. The flexibility and ease of use of symlinks make them a powerful tool in the Linux file system management arsenal.

Summary

In this lab, you learned how to create and manage symbolic links (symlinks) in Linux. Symlinks are a special type of file that act as a reference to another file or directory, allowing you to access the target file or directory through the symlink. You created a symlink to a file, and observed how changes to the target file are reflected in the symlink. Additionally, you learned that when the target file is deleted, the symlink remains but points to a non-existent file. The lab also covered the difference between symbolic and hard links, and provided practical use cases for symlinks.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like