How to Check Symbolic Links in Linux: A Practical Guide

Learn how to check symbolic links in Linux using ls -l, readlink, and stat. This step-by-step guide shows verification, resolving targets, and handling broken links with practical tips from All Symbols.

All Symbols
All Symbols Editorial Team
·5 min read
Symbolic Links in Linux - All Symbols
Quick AnswerSteps

You will learn how to check symbolic links in Linux and verify their targets. This quick guide demonstrates practical commands such as ls -l, readlink, and stat to inspect link destinations, identify broken links, and resolve relative paths. No root access is required for basic checks, but some directories may require elevated permissions.

Symbolic links, also called symlinks, serve as pointers. When you perform how to check symbolic link in linux, you verify the link’s destination and ensure the target still exists. This quick refresher sets the stage for safe, reliable file navigation and scripting. Understanding these basics helps students, researchers, and designers ensure their workflows don’t break when a linked file moves or is deleted. In practice, you’ll learn to distinguish between absolute and relative targets, confirm the link type, and prepare for automated checks in scripts.

A symbolic link is a special file that references another path in the filesystem. It can point to a file or a directory, and it may use an absolute path (starting at /) or a relative path (relative to the link’s directory). Linux distinguishes symbolic links from hard links, which are different directory entries for the same inode. When you check a symlink, you’re primarily asking: what target does this link refer to, and is it still valid? This matters for everything from system scripts to data pipelines, where broken links can cause failures or misdirected data.

The quickest way to inspect a symbolic link is to use ls -l, which displays the link’s permissions and its destination. For example, ls -l /path/to/link shows a line like lrwxrwxr-x 1 user user 12 date link -> /target/path. To reveal only the target, use readlink /path/to/link. If you need an absolute path, readlink -f or realpath will resolve the full path, including dereferencing intermediate links. To test whether a path is a symlink, use test -L /path/to/link. To check whether the link’s target exists, combine readlink with test -e or use stat to verify the target’s status. Examples:

Bash
# basic link info ls -l /path/to/link # reveal target readlink /path/to/link # absolute target readlink -f /path/to/link realpath /path/to/link # is it a symlink? [ -L /path/to/link ] && echo "symlink" || echo "not symlink" # does the target exist? test -e "$(readlink -f /path/to/link)" && echo "target exists" || echo "target missing"

Tools & Materials

  • Linux terminal (bash/zsh)(Accessible shell to run commands like ls, readlink, stat)
  • Text editor(Optional for annotating paths or writing scripts)
  • Permissions to access all targets(Some directories require elevated permissions; sudo may be needed)
  • Core command set (ls, readlink, realpath, stat, find)(Usually available by default on Linux distros)
  • Understanding of filesystem layout(Helpful for interpreting absolute vs relative paths)

Steps

Estimated time: 15-25 minutes

  1. 1

    Open the terminal and locate the link

    Launch your shell and navigate to the directory containing the symlink you want to inspect. Knowing the path to the link is essential before using inspection commands.

    Tip: Keep the path handy to avoid repeated cd commands.
  2. 2

    Check basic link properties with ls -l

    Run ls -l /path/to/link to see the link destination and confirm it is a symlink (starts with l in permissions).

    Tip: Look for the arrow '->' indicating the target.
  3. 3

    Reveal the target with readlink

    Use readlink /path/to/link to print the immediate target path. This is the simplest way to see where the link points.

    Tip: If you need an absolute path, use readlink -f or realpath.
  4. 4

    Verify the target exists

    Test whether the target exists with test -e "$(readlink -f /path/to/link)" or by using stat on the resolved path.

    Tip: A missing target means the symlink is broken.
  5. 5

    Differentiate absolute vs relative targets

    Check whether the reported target begins with / (absolute) or not (relative). Relative targets are interpreted from the link’s directory.

    Tip: Relative targets can become invalid if the link is moved.
  6. 6

    Batch-check a directory recursively

    Use find to list links and verify each target, e.g., find /path -type l -print | while read l; do echo -n "$l -> "; readlink -f "$l"; done.

    Tip: Limit scope to avoid excessive runtime on large trees.
  7. 7

    Document results and consider automation

    Record findings in a log or report and consider wrapping the checks in a script for consistency across projects.

    Tip: Automation reduces human error and speeds up audits.
Pro Tip: Prefer readlink -f or realpath to resolve canonical paths before tests.
Warning: Be careful when following links across network mounts or restricted directories; permissions may block checks.
Note: Test scripts on a staging copy to prevent accidental data loss.

Questions & Answers

What is a symbolic link in Linux?

A symbolic link is a special file that points to another path in the filesystem. It acts like a shortcut, enabling quick access to the target without duplicating data.

A symbolic link in Linux is a shortcut to another file or directory, pointing you to the real location.

How can I tell if a symlink is broken?

A symlink is broken when its target no longer exists. You can check with readlink -f or test -e on the resolved path to confirm. If the target is missing, you’ll need to update or remove the link.

If the target path doesn’t exist, the symlink is broken.

What’s the difference between a symlink and a hard link?

A symlink is a pointer to another path, possibly across filesystems. A hard link is another directory entry for the same inode within the same filesystem. Hard links can’t reference directories in most cases.

A symlink points to a path; a hard link is another name for the same file within the same filesystem.

Can I recursively check symlinks in a directory?

Yes. Use find to list symlinks and then validate targets with readlink -f. You can script this to scan large projects efficiently.

You can scan a directory tree for symlinks and verify each target with readlink -f.

What tools are best for symlink verification?

Core utilities like ls, readlink, realpath, and find are typically best. They’re standard on most Linux systems and work well in scripts.

Common tools such as ls, readlink, realpath, and find are usually all you need.

Watch Video

The Essentials

  • Identify each symlink with ls -l
  • Use readlink to reveal targets
  • Verify existence with test -e or stat
  • Differentiate absolute vs relative targets
  • Automate checks with a small script
Process diagram for checking symbolic links in Linux
Process flow: check, inspect, and verify Linux symbolic links

Related Articles