When viewing files in Linux, we often use the <span>ll</span> command (which is actually an alias for <span>ls -l</span>). You may have noticed that sometimes a file or directory is followed by an arrow and a path, for example:
lrwxrwxrwx 1 root root 11 Jun 15 14:32 lib64 -> usr/lib64
What does the -> mean here?
Meaning of the -> Symbol
This symbol indicates that the file is a soft link (symbolic link), similar to a shortcut in Windows systems.
The path after the arrow is the actual target that the soft link points to. In the example above, <span>lib64</span> is a soft link that points to <span>usr/lib64</span>, which is the actual directory. The file type field (the first field) shows as l (as in the example lrwxrwxrwx).
Soft Links vs Hard Links
There are two types of links in Linux:
Characteristics of Soft Links (Symbolic Links):
-
It is a special file that stores the path of the target file
-
Can span file systems
-
Can point to directories
-
If the original file is deleted, the soft link becomes invalid (known as a “broken link”)
Characteristics of Hard Links:
-
It is another entry for the original file, sharing the same inode number
-
Cannot span file systems
-
Cannot point to directories
-
If the original file is deleted, the hard link can still access the data
Creating a Soft Link
Use the <span>ln -s</span> command:
# Create a soft link
ln -s target_file link_name
# Example: Create a soft link for a frequently accessed directory
# This command creates a symbolic link at /path/to/symlink pointing to /path/to/original/file.
ln -s /path/to/original/file /path/to/symlink
Notes:
-
The permissions of the soft link file itself are usually 777, but the actual permissions are determined by the target file it points to
-
Moving or deleting the target file will cause the soft link to become invalid
-
Using
<span>ls -l</span>can show the location the soft link points to
Methods to View the Actual Location of the Link
Method 1: Use the <span><span>ls -l</span></span> Command (Most Common)
This is the most straightforward method. The <span>ls -l</span> (or its alias <span>ll</span>) will directly display the link file and its target in the output. Here, we just need to add the name of the soft link file after the ll command.
# Command format
ls -l filename
# or
ll filename
# Example
ls -l /usr/bin/python
# Terminal output
lrwxrwxrwx 1 root root 9 Apr 5 2022 /usr/bin/python -> python3.10
Here, the <span>-></span> after the arrow points to <span>python3.10</span>, which is the actual target of the symbolic link <span>/usr/bin/python</span>.
Advantages: Simple and easy to remember, and you can also see file permissions, owner, size, and other information at the same time.Disadvantages: If there is a lot of output, you need to manually search for the <span>-></span> symbol.
Method 2: Use the <span><span>readlink</span></span> Command (Most Professional)
<span>readlink</span> is a command specifically designed to resolve symbolic links, and its output is very clean, showing only the absolute or relative path that the link points to.
# Command
readlink filename
# Example
readlink /usr/bin/python
# Terminal output
python3.10
If you want to always display the absolute path (very useful), you can add the -f (or –canonicalize) option:
# Command
readlink -f filename
# Example
readlink -f /usr/bin/python
# Terminal output
/usr/bin/python3.10
Advantages: Clean output, very suitable for use in scripts or in conjunction with other commands (for example, <span>cd $(readlink -f mylink)</span><span><span>).</span></span><strong><span>Disadvantages:</span></strong><span> Requires remembering an additional command.</span>
Method 3: Use the file Command
The file command is used to determine the file type, and it will also provide clear information for symbolic links.
# Command
file filename
# Example
file /usr/bin/python
# Terminal output
/usr/bin/python: symbolic link to python3.10
Advantages: Clearly tells you that the file type is “symbolic link”.Disadvantages: Output is not as concise as <span>readlink</span><span><span>.</span></span>
Method 4: Directly Use <span><span>cd</span></span> and <span><span>pwd</span></span><code><span><span> (For Directory Links)</span></span>
If you have a symbolic link pointing to a directory and want to directly enter its actual location, you can use the <span>cd</span> command with the <span>-P</span> (Physical) option. This will ignore the symbolic link and switch directly to the physical path.
# Command: cd -P directory_link_name
pwd
# Example: Suppose there is a link mydata -> /media/true_data
cd mydata # At this point, you will be in the mydata/ directory
pwd # Output might be /home/user/mydata
cd -P mydata # Using cd -P pwd # Output will be /media/true_data
Tips: If the target of the symbolic link no longer exists, these commands will still work normally (ls -l will show a path pointing to a non-existent location, and readlink will also print the invalid path). This situation is known as a “broken link”.
Thank you for your attention and support. I will update various useful content from time to time. If you have any questions or different opinions, feel free to comment or leave a message for discussion and learning together!
Welcome to follow the public account, give a “Share“, “Like“, or “View“~Wishing you successful compilation and debugging!