How to check if a symbolic link exists in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a file in Linux is a symbolic link. You will explore three different methods to achieve this.

First, you will use the test command with the -L option to programmatically check the file type. Then, you will verify the link information by examining the output of the ls -l command. Finally, you will learn how to resolve the target file or directory that a symbolic link points to using the readlink command.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicSystemCommandsGroup -.-> linux/test("Condition Testing") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/ln("Link Creating") subgraph Lab Skills linux/echo -.-> lab-558768{{"How to check if a symbolic link exists in Linux"}} linux/test -.-> lab-558768{{"How to check if a symbolic link exists in Linux"}} linux/ls -.-> lab-558768{{"How to check if a symbolic link exists in Linux"}} linux/ln -.-> lab-558768{{"How to check if a symbolic link exists in Linux"}} end

In this step, you will learn how to test if a file is a symbolic link using the test command with the -L option.

A symbolic link (or symlink) is a special type of file that points to another file or directory. Think of it like a shortcut in Windows. When you access a symbolic link, you are actually accessing the file or directory it points to.

First, let's create a simple file. Make sure you are in the ~/project directory.

echo "This is the original file." > original_file.txt

Now, let's create a symbolic link to original_file.txt. We will name the symbolic link my_symlink.

ln -s original_file.txt my_symlink
  • ln: The command used to create links.
  • -s: This option tells ln to create a symbolic link (instead of a hard link).
  • original_file.txt: The target file that the symbolic link will point to.
  • my_symlink: The name of the symbolic link we are creating.

Now, we can use the test command to check if my_symlink is a symbolic link. The test command is used to check file types and compare values. It doesn't produce output directly, but its exit status indicates success (0) or failure (non-zero).

We can combine test with echo to see the result.

test -L my_symlink && echo "my_symlink is a symbolic link" || echo "my_symlink is NOT a symbolic link"
  • test -L my_symlink: This checks if my_symlink is a symbolic link.
  • &&: This is a logical AND operator. If the command before && succeeds (exit status 0), the command after && is executed.
  • ||: This is a logical OR operator. If the command before || fails (exit status non-zero), the command after || is executed.

Since my_symlink is indeed a symbolic link, the output should be:

my_symlink is a symbolic link

Now, let's test a regular file, like original_file.txt:

test -L original_file.txt && echo "original_file.txt is a symbolic link" || echo "original_file.txt is NOT a symbolic link"

The output should be:

original_file.txt is NOT a symbolic link

This confirms that test -L correctly identifies symbolic links.

In this step, you will learn how to identify symbolic links and their targets using the ls -l command.

The ls command is used to list files and directories. The -l option provides a "long listing" format, which includes detailed information about each file, such as permissions, ownership, size, and modification time.

When you use ls -l on a symbolic link, it displays the link itself and also shows what it points to.

Make sure you are in the ~/project directory where you created original_file.txt and my_symlink in the previous step.

Now, run the ls -l command:

ls -l

You should see output similar to this:

total 4
-rw-rw-r-- 1 labex labex   25 Feb 28 10:00 original_file.txt
lrwxrwxrwx 1 labex labex   15 Feb 28 10:00 my_symlink -> original_file.txt

Let's break down the output for my_symlink:

  • The first character l indicates that this is a symbolic link. For a regular file, this would be -.
  • The permissions (rwxrwxrwx) are often shown as full permissions for symbolic links, but the actual permissions are determined by the target file.
  • The link count is typically 1 for a symbolic link.
  • The owner and group are labex.
  • The size (15) is the number of characters in the target path (original_file.txt).
  • The timestamp is the creation or modification time of the symbolic link itself.
  • my_symlink: The name of the symbolic link.
  • -> original_file.txt: This part clearly shows that my_symlink points to original_file.txt.

Compare this to the output for original_file.txt:

  • The first character - indicates that this is a regular file.
  • The permissions (rw-rw-r--) show who can read, write, and execute the file.
  • The link count (1) indicates how many hard links point to this file's data.
  • The size (25) is the actual size of the file content in bytes.

Using ls -l is a very common way to quickly identify symbolic links and see where they are pointing.

In this step, you will learn how to use the readlink command to find the target of a symbolic link.

While ls -l is great for a visual overview, the readlink command is specifically designed to resolve symbolic links and print their target path. This is particularly useful in scripts or when you need just the target path without other file information.

Make sure you are in the ~/project directory.

Use the readlink command on the symbolic link my_symlink that you created in the previous steps:

readlink my_symlink

The output will be the path that the symbolic link points to:

original_file.txt

This confirms that my_symlink is indeed pointing to original_file.txt.

The readlink command has a useful option, -f, which recursively follows all symbolic links and resolves them to the absolute path of the final target. This is helpful if you have a chain of symbolic links.

Let's create another symbolic link that points to my_symlink:

ln -s my_symlink another_link

Now, if you use readlink on another_link without the -f option, it will show the immediate target:

readlink another_link

Output:

my_symlink

But if you use readlink -f on another_link, it will follow the chain (another_link -> my_symlink -> original_file.txt) and give you the absolute path of the final target:

readlink -f another_link

Output:

/home/labex/project/original_file.txt

This shows the full path to the original file. The readlink -f command is very powerful for finding the ultimate destination of a link, even if it's nested within other links.

You have now learned three ways to work with symbolic links: using test -L to check if a file is a link, using ls -l to see link details and targets, and using readlink to resolve the target path.

Summary

In this lab, you learned how to check if a file is a symbolic link in Linux. You first created a regular file and a symbolic link pointing to it. You then used the test command with the -L option to programmatically determine if a given file is a symbolic link, observing how its exit status can be used with logical operators (&& and ||) to display a result.

You also learned how to visually verify a symbolic link using the ls -l command, which displays a 'l' at the beginning of the permissions field and shows the link target. Finally, you explored the readlink command to resolve the target path of a symbolic link, understanding its usefulness for scripting and automation.