To view symlinks in Linux, use ls -l to see a link and its target, or use readlink to print the target path. These commands help you identify what a symbolic link points to, check whether that target exists, and investigate links that behave unexpectedly.
You need a terminal and permission to access the directory or link you want to inspect. A symlink, short for symbolic link, is a filesystem object that refers to another path; it is not the same as a regular file or a hard link. The steps below cover one link, folders of links, target resolution, and broken links.
What You’ll Need
- A Linux system with access to a shell, such as a terminal window or a text console.
- The path to a symlink, or the directory where you want to look for symlinks.
- Basic familiarity with commands such as
cdandls. You do not normally need administrator privileges just to view a link.
Step-by-Step
-
Open a terminal and identify the path. If you know the link’s name, note its location. For example, a link named
currentin a directory namedprojectcan be inspected withls -l project/current. If you are already in the directory, usels -l current. Relative paths are interpreted from your current working directory, so use an absolute path if you are unsure where you are. -
Use
ls -lto see the link and its destination. Runls -l path/to/link, replacing the example path with the real one. A symlink line begins withlin the permissions field and normally includes an arrow, followed by the target path. For example,lrwxrwxrwx ... current -> releases/stableindicates thatcurrentpoints toreleases/stable. The exact owner, date, and permission display can vary by system and options. -
Read the target alone with
readlink. Runreadlink path/to/linkto print the path stored in the symlink. This is useful when you want a simple result without the rest of the directory listing. The output may be relative, such as../shared/config; that path is interpreted relative to the directory containing the symlink, not relative to the directory from which you ran the command. -
Ask for the resolved target path when needed. Use
readlink -f path/to/linkto print a canonicalized path, where supported. This can make it easier to understand where a chain of links leads. The target generally needs to exist for full resolution, so this command may not print a usable result for a broken link. GNU and other implementations can differ in details; consult the command’s local manual if an option is unavailable. -
Check whether the target exists. Use
test -e path/to/linkto test whether following the link reaches an existing file or directory. A successful test produces no text, so check the exit status withecho $?:0means the test succeeded, while a nonzero result means it did not. To test whether the path itself is a symlink, usetest -L path/to/link. These tests answer different questions: a broken symlink can pass the symlink test but fail the existence test. -
Look for symlinks in a directory. To list entries in the current directory, run
ls -land look for lines whose first permission character isl. For a clearer check of a particular entry, usetest -L name. To search below a directory for symlinks, usefind path/to/directory -type l. This prints matching link paths, including links that point to missing targets. -
Inspect a link without accidentally listing its target instead. When you pass a symlink to
ls -l, the long listing shows the link itself and its target. By contrast, commands or options that follow symlinks may show information about the destination. If you need to distinguish the link from the target, usels -ld path/to/linkorstat path/to/link. The precise fields instatvary, but it can help identify the object type and metadata. -
Compare the link path with its destination. If a link points to a relative path, resolve that path from the directory containing the link. For example, if
/home/lee/project/currentpoints to../releases/stable, the destination is reached by starting in/home/lee/projectand following that relative path. For a link that points to another symlink, repeat the inspection or usereadlink -fwhere available to follow the chain.
Common Mistakes
- Confusing the link with its target. A link has its own pathname, while the target is the path it refers to.
ls -ldisplays both;readlinkprints the stored target path. - Treating a relative target as relative to the terminal’s current directory. A relative target is interpreted from the directory containing the symlink. This is why the same target text can make sense in one location and fail in another.
- Assuming that every symlink points to something that exists. A link can remain after its destination is moved or removed. It is still a symlink, even when following it fails.
- Using a file-existence check to identify links.
test -efollows a link and checks its destination. Usetest -Lwhen you need to know whether the path itself is a symlink. - Expecting
readlinkto show an absolute path. It often prints exactly the target text stored in the link, which may be relative. Use a resolving option when you need a canonical path and the target is available. - Overlooking spaces or special characters in names. Put a path in quotes, such as
ls -l "my link", so the shell treats it as one argument. Use tab completion to reduce typing mistakes.
Tips
- Start with
ls -lfor a quick visual check, then usereadlinkif you want only the destination text. - Use an absolute path when investigating a confusing link. It removes uncertainty about your current directory.
- Use
ls -ldwhen you want details about a directory symlink itself. Without the option, a listing may show the contents reached through the link, which can obscure what you meant to inspect. - Remember that a symbolic link can point to a file, a directory, another symlink, or a path that does not currently exist.
- Before changing or deleting a link, inspect it first. Removing a symlink and removing its target are different operations; commands that follow links can affect the destination instead.
- For command options on your system, read the local manual with
man ls,man readlink, orman find. Available options and behavior can differ between Linux distributions and command implementations.
Troubleshooting
The command says “No such file or directory.” Check for a misspelled name, an incorrect directory, or a path that is relative to a different working directory than you expected. Run pwd to see your current directory, then try the link’s absolute path. If the link exists but its target does not, ls -l may still show the link while attempts to follow it fail.
readlink prints nothing. The path may not be a symlink, or it may be inaccessible. Confirm with ls -ld path and check the first character of the permissions field. If it is not l, the object is not a symbolic link. Also verify that you supplied the link’s path rather than a path to its destination.
readlink -f is unavailable or returns no resolved path. Some systems provide a different version of the command, and resolution can fail when a target is missing or a path component cannot be accessed. Try plain readlink to inspect the stored target, then check each part of that path. Use the local manual to see which options your version supports.
You can see the link but cannot access the destination. A link does not grant extra permission to read its target. Check whether you can access the target directory and file, and whether the target path is valid. If access is denied, ask the system administrator or file owner to review permissions rather than changing permissions blindly.
A search misses a link you expected to find. Confirm the search starting directory and remember that find path -type l reports symlinks beneath that path. A link outside the searched tree will not appear. Also distinguish between finding links and following them: the command shown finds symlink entries rather than searching through each link’s destination.
Frequently Asked Questions
How can I tell whether a path is a symlink?
Run ls -ld path and look for l at the start of the permissions field, or check with test -L path. The check applies to the path itself, even if its target is missing.
How do I see where a symlink points?
Use ls -l path to see the link and its target on one line, or use readlink path to print the stored target path by itself.
How do I get the full path of a symlink’s target?
Try readlink -f path. It resolves the path where supported, but it may not produce a result if the destination is missing or cannot be traversed.
How do I find all symlinks under a directory?
Run find directory -type l, replacing directory with the location to search. It finds symlink entries below that starting point, including broken links.
Can a symlink be broken but still visible?
Yes. The symlink itself can remain after its target is moved or deleted. ls -l can show the link, while following it or testing it with test -e fails.
Does viewing a symlink change anything?
Commands such as ls -l, readlink, and find only inspect paths; they do not create or remove links. Be cautious with separate commands that write, move, or delete files, because their behavior depends on whether they follow the link.