Symbolic links (symlinks) are special files that act as pointers to other files or directories. They're widely used in Linux/Unix systems for creating shortcuts or maintaining multiple references to a single file. Unlike hard links, symlinks can cross filesystem boundaries and link to directories.
The most straightforward way to view a symlink's target is using the ls
command with the -l
option:
ls -l /root/Public/myothertextfile.txt
This will display output like:
lrwxrwxrwx 1 root root 21 Jan 1 10:00 myothertextfile.txt -> /root/Public/mytextfile.txt
The arrow (->
) indicates the symlink target.
For scripting purposes or when you need just the target path without additional information, readlink
is the preferred tool:
readlink /root/Public/myothertextfile.txt
Output:
/root/Public/mytextfile.txt
For more complex scenarios, consider these approaches:
# Follow all symlinks recursively
readlink -f /root/Public/myothertextfile.txt
# Canonicalize path (resolve all symlinks)
readlink -e /root/Public/myothertextfile.txt
# Check if multiple files are symlinks
file /root/Public/myothertextfile.txt /another/file
Here's how you might use these commands in a real-world script:
#!/bin/bash
LINK="/root/Public/myothertextfile.txt"
TARGET=$(readlink "$LINK")
if [ -z "$TARGET" ]; then
echo "Error: $LINK is not a symlink" >&2
exit 1
fi
if [ ! -e "$TARGET" ]; then
echo "Warning: Target $TARGET doesn't exist" >&2
fi
echo "Symlink $LINK points to $TARGET"
Sometimes you might encounter:
- Broken symlinks (target doesn't exist)
- Recursive symlinks (A->B->A)
- Relative path symlinks
Use readlink -f
to handle most edge cases properly.
Other commands that can display symlink information:
# Using stat
stat -c %N /root/Public/myothertextfile.txt
# Using find to locate symlinks
find /root/Public -type l -exec ls -la {} \;
Symbolic links (symlinks) are special files that act as pointers to other files or directories. They're incredibly useful for creating shortcuts or maintaining multiple references to a single file.
There are several command-line tools available in Linux to inspect symbolic links:
# Basic ls command with -l flag
ls -l /path/to/symlink
# Using readlink command
readlink /path/to/symlink
# Using stat command
stat /path/to/symlink
Let's examine each method with practical examples:
# Create a sample symlink
ln -s /var/log/syslog ~/syslog_link
# Method 1: Using ls -l
ls -l ~/syslog_link
# Output: lrwxrwxrwx 1 user user 13 Jun 1 10:00 /home/user/syslog_link -> /var/log/syslog
# Method 2: Using readlink
readlink ~/syslog_link
# Output: /var/log/syslog
# Method 3: Using stat
stat ~/syslog_link
# Output shows the link target in the information display
For scripts and automation, you might want these variations:
# Get absolute path of target
readlink -f ~/syslog_link
# Check if a file is symlink
if [ -L ~/syslog_link ]; then
echo "This is a symbolic link"
fi
# Find all symlinks in a directory
find /path/to/dir -type l -exec ls -la {} \;
Remember these important notes:
- Broken symlinks will still show the original target path
- Circular references can occur if not careful
- Permissions on symlinks behave differently than regular files
Here's a practical script to check multiple symlinks:
#!/bin/bash
check_symlinks() {
for link in "$@"; do
if [ -L "$link" ]; then
echo "Link: $link"
echo "Target: $(readlink "$link")"
echo ""
else
echo "$link is not a symbolic link"
fi
done
}
# Usage
check_symlinks ~/syslog_link ~/another_link /etc/alternatives/java
This script will output the target of each symlink passed as argument, or indicate if the file isn't a symlink.