How to Find the Target of a Symbolic Link

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial will guide you through the process of finding the target of a symbolic link on your Linux system. Symbolic links, also known as symlinks, are special types of files that act as pointers to other files or directories. They are commonly used in Linux systems to create shortcuts, organize files, and manage software versions.

By the end of this tutorial, you will be able to identify symbolic links and determine the files or directories they point to using various Linux commands. This knowledge is essential for effective file system navigation and management in Linux environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/ln("Link Creating") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/FileandDirectoryManagementGroup -.-> linux/find("File Searching") subgraph Lab Skills linux/ls -.-> lab-392854{{"How to Find the Target of a Symbolic Link"}} linux/ln -.-> lab-392854{{"How to Find the Target of a Symbolic Link"}} linux/cat -.-> lab-392854{{"How to Find the Target of a Symbolic Link"}} linux/cd -.-> lab-392854{{"How to Find the Target of a Symbolic Link"}} linux/find -.-> lab-392854{{"How to Find the Target of a Symbolic Link"}} end

Symbolic links (symlinks) are special files in Linux that point to other files or directories. They function similar to shortcuts in other operating systems but are more powerful and integrated into the file system.

Let's start by exploring the symbolic links that have been set up for this tutorial. First, navigate to the project directory:

cd ~/project/symlink-tutorial

Now, let's list the files in this directory to see what we have:

ls -la

You should see output similar to this:

total 20
drwxr-xr-x 3 labex labex 4096 Jul 15 10:00 .
drwxr-xr-x 3 labex labex 4096 Jul 15 10:00 ..
lrwxrwxrwx 1 labex labex   12 Jul 15 10:00 broken-link.txt -> ../nonexistent-file.txt
lrwxrwxrwx 1 labex labex   13 Jul 15 10:00 dir-link -> test-directory
lrwxrwxrwx 1 labex labex   13 Jul 15 10:00 nested-link.txt -> simple-link.txt
-rw-r--r-- 1 labex labex   35 Jul 15 10:00 original.txt
lrwxrwxrwx 1 labex labex   10 Jul 15 10:00 passwd-link -> /etc/passwd
lrwxrwxrwx 1 labex labex   12 Jul 15 10:00 simple-link.txt -> original.txt
drwxr-xr-x 2 labex labex 4096 Jul 15 10:00 test-directory

Notice that symbolic links have a different appearance in the listing:

  • They start with l in the file permissions (first column)
  • Their names are followed by an arrow (->) pointing to their target
  • They typically have permissions lrwxrwxrwx

Let's examine the simple-link.txt file, which is a symbolic link to original.txt. You can see the contents of this link by using the cat command:

cat simple-link.txt

Output:

This is the original file content.

Even though you're accessing simple-link.txt, you're actually seeing the contents of original.txt. This is how symbolic links work - they transparently redirect access to their target.

Now, let's also try the directory symbolic link:

ls -la dir-link

Output:

total 12
drwxr-xr-x 2 labex labex 4096 Jul 15 10:00 .
drwxr-xr-x 3 labex labex 4096 Jul 15 10:00 ..
-rw-r--r-- 1 labex labex   42 Jul 15 10:00 test-file.txt
lrwxrwxrwx 1 labex labex   16 Jul 15 10:00 relative-link.txt -> ../original.txt

As you can see, when you list the contents of dir-link, you're actually seeing the contents of the test-directory that it points to.

The ls command with specific options is one of the simplest ways to identify symbolic links and see what they point to. Let's explore various ways to use ls for this purpose.

Listing with the -l Option

The -l option provides a detailed listing, showing the link and its target:

ls -l simple-link.txt

Output:

lrwxrwxrwx 1 labex labex 12 Jul 15 10:00 simple-link.txt -> original.txt

The arrow (->) shows that simple-link.txt points to original.txt.

Listing All Files with the -la Option

The -la options combined list all files (including hidden ones) with detailed information:

ls -la

This shows all files in the current directory, including symbolic links and their targets.

Color-Coded Listing

Most modern Linux distributions configure ls to display different file types in different colors. Symbolic links are typically shown in light blue or cyan. You can use:

ls --color=always

Output will show symbolic links in a distinct color.

You can use the find command to list only symbolic links in the current directory:

find . -maxdepth 1 -type l

Output:

./broken-link.txt
./dir-link
./nested-link.txt
./passwd-link
./simple-link.txt

This command lists all symbolic links in the current directory (.) without descending into subdirectories (-maxdepth 1).

Let's look at nested-link.txt, which points to another symbolic link:

ls -l nested-link.txt

Output:

lrwxrwxrwx 1 labex labex 13 Jul 15 10:00 nested-link.txt -> simple-link.txt

And simple-link.txt points to:

ls -l simple-link.txt

Output:

lrwxrwxrwx 1 labex labex 12 Jul 15 10:00 simple-link.txt -> original.txt

So nested-link.txt points to simple-link.txt, which in turn points to original.txt. This is an example of a chain of symbolic links.

While the ls command can show you what a symbolic link points to, the readlink command is specifically designed for this purpose. It prints the value of a symbolic link or canonical file name.

The most basic use of readlink is to provide the name of the symbolic link:

readlink simple-link.txt

Output:

original.txt

This shows that simple-link.txt points to original.txt.

For nested symbolic links, the basic readlink command only gives you the immediate target:

readlink nested-link.txt

Output:

simple-link.txt

To follow the entire chain of links to the final target, use the -f (follow) option:

readlink -f nested-link.txt

Output:

/home/labex/project/symlink-tutorial/original.txt

This shows the full path to the final target file.

Dealing with Relative Paths

Let's examine a symbolic link with a relative path:

ls -l test-directory/relative-link.txt

Output:

lrwxrwxrwx 1 labex labex 16 Jul 15 10:00 test-directory/relative-link.txt -> ../original.txt

Using readlink with the -f option resolves the relative path:

readlink -f test-directory/relative-link.txt

Output:

/home/labex/project/symlink-tutorial/original.txt

Sometimes, symbolic links point to files that don't exist. Let's check our broken link:

readlink broken-link.txt

Output:

../nonexistent-file.txt

The readlink command still shows what the broken link points to, even though the target doesn't exist.

Using readlink -f with a broken link will try to resolve the path but still indicate that it's broken:

readlink -f broken-link.txt

Output:

/home/labex/project/nonexistent-file.txt

The readlink command offers several useful options:

  • -e: Return the canonicalized path only if it exists
  • -m: Do not dereference symlinks, display symlink path even if pointing to nonexistent file
  • -n: Do not output the trailing newline
  • -v: Verbose output

Example using the -e option:

readlink -e broken-link.txt

This will give no output since the target file doesn't exist.

Besides ls and readlink, there are several other commands you can use to determine the target of a symbolic link. Let's explore these alternatives.

Using the file Command

The file command identifies the type of a file, including symbolic links:

file simple-link.txt

Output:

simple-link.txt: symbolic link to original.txt

This clearly indicates that simple-link.txt is a symbolic link pointing to original.txt.

Let's try it with a directory link:

file dir-link

Output:

dir-link: symbolic link to test-directory

The file command works with both file and directory links.

Using the realpath Command

The realpath command displays the resolved path of a file, which is useful for finding the final target of symbolic links:

realpath simple-link.txt

Output:

/home/labex/project/symlink-tutorial/original.txt

This shows the absolute path to the target file.

For nested links, realpath automatically follows the entire chain:

realpath nested-link.txt

Output:

/home/labex/project/symlink-tutorial/original.txt

Using stat Command

The stat command provides detailed information about a file, including whether it's a symbolic link:

stat simple-link.txt

Output (partial):

  File: simple-link.txt -> original.txt
  Size: 12        	Blocks: 0          IO Block: 4096   symbolic link
...

This output shows that simple-link.txt is a symbolic link pointing to original.txt.

Let's create a new symbolic link to practice with:

ln -s /etc/hostname my-hostname

This creates a symbolic link called my-hostname that points to the /etc/hostname file.

Now, let's use the commands we've learned to examine this new link:

ls -l my-hostname

Output:

lrwxrwxrwx 1 labex labex 12 Jul 15 11:00 my-hostname -> /etc/hostname
readlink my-hostname

Output:

/etc/hostname
file my-hostname

Output:

my-hostname: symbolic link to /etc/hostname
cat my-hostname

Output (example, your hostname may be different):

labex-ubuntu

By using these various methods, you can easily find the target of any symbolic link in your Linux system.

Practical Applications and Advanced Usage

Now that you understand how to find the target of symbolic links, let's explore some practical applications and advanced usage scenarios.

For deeply nested links (a link pointing to another link, which points to another link, and so on), the -f option of readlink is essential:

## Create a chain of links
ln -s original.txt link1.txt
ln -s link1.txt link2.txt
ln -s link2.txt link3.txt

## Check the chain
readlink -f link3.txt

Output:

/home/labex/project/symlink-tutorial/original.txt

To find all symbolic links in a directory and its subdirectories:

find /home/labex/project/symlink-tutorial -type l

This command searches for all items of type l (symbolic links) in the specified directory and its subdirectories.

To find all symbolic links and see what they point to:

find /home/labex/project/symlink-tutorial -type l -ls

This command combines find with the -ls option to provide a detailed listing of each symbolic link.

When you modify a file through a symbolic link, you're actually modifying the target file. Let's demonstrate this:

## Display the original content
cat original.txt

Output:

This is the original file content.
## Append to the file through the symbolic link
echo "Line added through symlink." >> simple-link.txt

## Check the original file
cat original.txt

Output:

This is the original file content.
Line added through symlink.

The change made through the symbolic link appears in the original file.

If you need to update a symbolic link to point to a different target, you can use the -f option with ln -s:

## Create a new file
echo "This is a new target file." > new-target.txt

## Update the symlink
ln -sf new-target.txt simple-link.txt

## Check what the link points to now
readlink simple-link.txt

Output:

new-target.txt

Cleaning Up

Let's clean up the files we created in this step:

rm link1.txt link2.txt link3.txt new-target.txt

Symbolic links are useful in many situations:

  • Creating shortcuts to frequently accessed files or directories
  • Maintaining multiple versions of files or software
  • Creating more intuitive file paths
  • Linking to configuration files
  • Organizing files across different file systems

By mastering the techniques for finding symbolic link targets, you'll be better equipped to manage and navigate the Linux file system effectively.

Summary

In this tutorial, you've learned how to find the target of symbolic links in Linux using various methods:

  1. Using the ls -l command to identify symbolic links and see their targets
  2. Using the readlink command to print the value of a symbolic link
  3. Using readlink -f to follow the entire chain of symbolic links to the final target
  4. Exploring alternative methods like file, realpath, and stat
  5. Working with practical applications and advanced usage scenarios

These skills are essential for effective file system management in Linux. Symbolic links provide flexibility and convenience in organizing files and directories, and understanding how to work with them is an important part of Linux system administration and everyday usage.

Now you have the knowledge to confidently identify, create, and manage symbolic links in your Linux environment.