How to Check and Resolve Symbolic Links with readlink in Bash

Symbolic link connecting a deployed configuration file to its real target in a versioned directory

What You’ll Learn

In this lesson, you will learn how to inspect symbolic links and find the real files they point to with the Bash readlink command. This is useful when configuration files and scripts are deployed through release directories or other indirect paths.

  • Understand what a symbolic link is.
  • Use readlink to display a link’s stored target.
  • Use readlink -f to find the final absolute path.
  • Safely check where deployed configuration files and scripts are located.
Ad

The Concept

A symbolic link, often called a symlink, is a special filesystem entry that points to another file or directory. It works like a shortcut, but it is managed by the filesystem and is commonly used in Linux deployments.

For example, a deployment might store versioned files like this:

  • releases/v2/app.conf contains the real configuration.
  • config/app.conf is a symbolic link used by applications.

The application can always use config/app.conf, while administrators can update the link to point to a newer release.

The basic command is:

readlink path-to-link

This displays the target stored in the symbolic link. If you need the final, absolute location after following one or more links, use the -f option:

readlink -f path-to-link

On Linux, readlink -f follows the complete path and resolves relative path components such as ... It is especially useful when you want to verify which deployed release a configuration file or script actually comes from.

Basic Example

The following example creates a small deployment-like directory. The visible configuration path is a symbolic link to a configuration file inside a release directory.

demo_dir="/tmp/dcg-readlink-demo"

rm -rf "$demo_dir"
mkdir -p "$demo_dir/releases/v2" "$demo_dir/config"

printf 'port=8080\n' > "$demo_dir/releases/v2/app.conf"
ln -s "../releases/v2/app.conf" "$demo_dir/config/app.conf"

config_link="$demo_dir/config/app.conf"

echo "Stored link target:"
readlink "$config_link"

echo "Final target path:"
readlink -f "$config_link"

Expected Output

Stored link target:
../releases/v2/app.conf
Final target path:
/tmp/dcg-readlink-demo/releases/v2/app.conf

How the Code Works

A deployed configuration path is checked to confirm it is a symbolic link. The link stores a relative target, which readlink displays; readlink -f follows that target and resolves the final absolute path to the real release file.
Check the deployed path, inspect its stored link target, then use readlink -f to resolve the actual release file.

The variable demo_dir stores the location of the example deployment:

demo_dir="/tmp/dcg-readlink-demo"

The script removes an earlier copy of the demonstration directory and creates the release and configuration directories. The files are placed under /tmp, so the example does not modify a normal system configuration directory.

This command creates the real configuration file:

printf 'port=8080\n' > "$demo_dir/releases/v2/app.conf"

The printf command writes one configuration setting into the release file. The > operator redirects the text into that file.

This command creates the symbolic link:

ln -s "../releases/v2/app.conf" "$demo_dir/config/app.conf"

The first argument is the link target, and the second argument is the name of the link. Because the target is relative, it is interpreted from the directory containing the link: /tmp/dcg-readlink-demo/config. From that directory, ../releases/v2/app.conf correctly reaches the release file.

This command displays the target exactly as it is stored in the link:

readlink "$config_link"

That output is a relative path. It tells you what the link contains, but it is not yet the final absolute location.

Adding -f follows the link and prints the resolved path:

readlink -f "$config_link"

This makes it easy to confirm which actual release file the application will use. If you also need timestamps or other metadata for the resolved file, the Bash stat command complements readlink.

Another Example

A common deployment pattern uses a current directory link to select the active release. Instead of changing every script path, an administrator updates one link when deploying a new version.

This example resolves the active release and then builds the path to its deployment script.

deployment_dir="/tmp/dcg-script-deployment"

rm -rf "$deployment_dir"
mkdir -p "$deployment_dir/releases/2025-03-01"

printf '#!/usr/bin/env bash\nprintf "Deployment started\\n"\n' > "$deployment_dir/releases/2025-03-01/start.sh"
ln -s "releases/2025-03-01" "$deployment_dir/current"

active_release=$(readlink -f "$deployment_dir/current")
script_path="$active_release/start.sh"

printf 'Active release: %s\n' "$active_release"
printf 'Deployment script: %s\n' "$script_path"

if [[ -f "$script_path" ]]; then
    echo "The deployment script exists."
fi

Here, current is a symbolic link to a directory rather than directly to a file. Resolving the directory first lets the script construct the full path to start.sh. This same approach can be used to locate the active configuration directory or other deployed files.

For managed Linux instances, you could run a similar inspection command remotely with AWS Systems Manager Run Command, avoiding the need to log in through SSH.

Common Mistakes

Using readlink when you need the final path

readlink link only displays the target stored in the link. That target may be relative, and it may point to another symbolic link. Use readlink -f link when you need the final absolute path.

Forgetting that relative targets depend on the link’s directory

A relative symlink target is not interpreted from your current working directory. It is interpreted from the directory that contains the symlink. This is why the example uses ../releases/v2/app.conf for a link inside the config directory.

Resolving a path that does not exist

On Linux, readlink -f expects the path to be resolvable. If a link is broken or a directory component is missing, it may produce no useful path and return a nonzero status. Check that the target exists when troubleshooting a deployment.

Confusing a regular file with a symbolic link

readlink is intended for links. A regular file may contain configuration text, but it does not have a link target. You can use [[ -L "$path" ]] in Bash to test whether a path is a symbolic link before resolving it.

Try It Yourself

Create a directory named /tmp/readlink-practice with these items:

  • A real file at releases/v1/service.conf.
  • A symbolic link at config/service.conf pointing to the real file.

Store the link path in a variable, then use both readlink and readlink -f. Compare the relative target shown by the first command with the absolute path shown by the second command.

Challenge

Build a small deployment layout in /tmp/dcg-challenge:

  • Create the real file at releases/v3/production.conf.
  • Create a config directory.
  • Create config/production.conf as a symbolic link to the release file.
  • Use readlink -f to resolve the link.
  • Print the resolved path only if the link is a symbolic link.

Solution

challenge_dir="/tmp/dcg-challenge"

rm -rf "$challenge_dir"
mkdir -p "$challenge_dir/releases/v3" "$challenge_dir/config"

printf 'environment=production\n' > "$challenge_dir/releases/v3/production.conf"
ln -s "../releases/v3/production.conf" "$challenge_dir/config/production.conf"

config_path="$challenge_dir/config/production.conf"

if [[ -L "$config_path" ]]; then
    resolved_path=$(readlink -f "$config_path")
    printf 'Resolved configuration: %s\n' "$resolved_path"
else
    echo "The configuration path is not a symbolic link."
fi

The [[ -L "$config_path" ]] test confirms that the path is a symbolic link. Only after that check does the script call readlink -f, then it prints the final absolute path to the deployed production configuration.

Key Takeaways

  • A symbolic link points to another file or directory.
  • readlink displays the target stored in a symbolic link.
  • readlink -f follows links and returns the final absolute path on Linux.
  • Relative link targets are interpreted from the directory containing the link.
  • Checking a link with [[ -L "$path" ]] helps avoid treating regular files as symbolic links.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad