Skip to main content

Ubuntu Symbolic Links

Introduction

Symbolic links (also called symlinks or soft links) are a powerful feature in Ubuntu and other Linux-based operating systems that allow you to create references to files and directories. Think of them as advanced shortcuts that can make your file management more efficient and organized.

Unlike regular files that contain data, a symbolic link contains a path pointing to another file or directory. When you access the symbolic link, the system automatically redirects you to the target file or directory.

Symbolic links offer several advantages:

  • Access files from multiple locations without creating duplicate copies
  • Create shortcuts to deeply nested directories
  • Maintain backward compatibility when reorganizing file structures
  • Simplify complex file paths with shorter, more convenient names
  • Link to files across different file systems

The ln command with the -s option is used to create symbolic links in Ubuntu. The basic syntax is:

bash
ln -s target_path link_path

Where:

  • target_path is the path to the original file or directory
  • link_path is the path where you want to create the symbolic link

Let's create a symbolic link to a file:

bash
# Create a sample file
echo "This is a sample file" > original.txt

# Create a symbolic link to the file
ln -s original.txt link_to_original.txt

# View the result
ls -l

Output:

total 4
lrwxrwxrwx 1 user user 12 Mar 13 14:30 link_to_original.txt -> original.txt
-rw-rw-r-- 1 user user 21 Mar 13 14:30 original.txt

Notice the -> symbol in the output, which indicates that link_to_original.txt is a symbolic link pointing to original.txt.

Creating symbolic links to directories works similarly:

bash
# Create a sample directory
mkdir documents

# Create a symbolic link to the directory
ln -s documents docs

# View the result
ls -l

Output:

total 4
lrwxrwxrwx 1 user user 9 Mar 13 14:35 docs -> documents
drwxrwxr-x 2 user user 4096 Mar 13 14:35 documents

Let's visualize the relationship between symbolic links and their targets:

When you access a symbolic link, the system follows the pointer to the target automatically.

Unlike hard links (created with ln without the -s option), symbolic links:

  • Can span different file systems
  • Can link to directories
  • Display as distinct file types in the file system
  • Become broken if the target is moved or deleted

2. File Permissions

Symbolic links have their own permissions, but when you access a file through a symbolic link, the permissions of the target file apply.

3. Size and Timestamps

The size of a symbolic link is typically the length of the path it stores, not the size of the target file. The timestamp shows when the link was created, not when the target was last modified.

Use ls -l to see which files are symbolic links:

bash
ls -l link_to_original.txt

Output:

lrwxrwxrwx 1 user user 12 Mar 13 14:30 link_to_original.txt -> original.txt

The l at the beginning of the permissions indicates a symbolic link.

Use the readlink command:

bash
readlink link_to_original.txt

Output:

original.txt

For the full resolved path, use:

bash
readlink -f link_to_original.txt

Output:

/home/user/original.txt

To remove a symbolic link, use the rm command just like with regular files:

bash
rm link_to_original.txt

Be careful to specify the name of the link itself, not the target. Adding a trailing slash can cause rm to try to delete the contents of the target directory instead of the link.

Practical Applications

1. Creating a Shortcut to a Deeply Nested Directory

bash
# Create a deeply nested directory structure
mkdir -p ~/projects/2025/website/data/logs

# Create a symbolic link for easier access
ln -s ~/projects/2025/website/data/logs ~/logs

# Now you can access the logs directory easily
cd ~/logs

2. Maintaining Compatibility When Reorganizing Files

bash
# Imagine you want to reorganize your scripts directory
mkdir -p ~/new_scripts_location

# Move your scripts
mv ~/scripts/* ~/new_scripts_location/

# Create a symbolic link at the old location
ln -s ~/new_scripts_location ~/scripts

# Now programs expecting scripts at ~/scripts will still work

3. Creating Multiple Access Points for Shared Resources

bash
# Create a symbolic link to a shared configuration file
ln -s /etc/shared_config.conf ~/my_config.conf

# Now you can access the shared configuration file from your home directory

4. Managing Different Versions of Software

bash
# Imagine you have multiple versions of a program
# /opt/app-v1.0
# /opt/app-v2.0

# Create a symbolic link that points to the current version
ln -s /opt/app-v2.0 /opt/app-current

# Update your scripts to use /opt/app-current
# When you want to switch versions, just update the symbolic link
ln -sf /opt/app-v1.0 /opt/app-current # The -f flag forces overwriting the existing link

If you delete or move the target file, the symbolic link becomes "broken":

bash
# Create a file and a symbolic link
echo "Test file" > target.txt
ln -s target.txt link_to_target.txt

# Remove the target file
rm target.txt

# Try to access the symbolic link
cat link_to_target.txt

Output:

cat: link_to_target.txt: No such file or directory

To find broken symbolic links in your current directory:

bash
find . -type l -! -exec test -e {} \; -print

Relative vs. Absolute Paths

When creating symbolic links, you can use either relative or absolute paths:

bash
# Absolute path (starts with /)
ln -s /home/user/documents/file.txt link1.txt

# Relative path (relative to the location of the symbolic link)
ln -s ../documents/file.txt link2.txt
  • Absolute paths will work regardless of where the symbolic link is moved
  • Relative paths will break if the symbolic link is moved to a different directory

Choose based on whether you expect to move the symbolic link in the future.

Summary

Symbolic links are a powerful tool in Ubuntu file management that allow you to:

  • Create references to files and directories
  • Access files from multiple locations without duplication
  • Simplify complex file paths
  • Maintain backward compatibility when reorganizing files

The basic command to create a symbolic link is ln -s target_path link_path. Remember to consider whether to use absolute or relative paths depending on your specific needs.

Exercises

  1. Create a symbolic link in your home directory that points to the /etc directory.
  2. Create a file and make two different symbolic links pointing to it. What happens when you modify the file through one of the links?
  3. Create a symbolic link using a relative path, then move the link to a different directory. Does it still work? Why or why not?
  4. Find all broken symbolic links in your home directory.
  5. Create a directory structure with at least three levels, then create a symbolic link that provides a shortcut to the deepest directory.

Additional Resources

  • man ln - The manual page for the ln command
  • man readlink - The manual page for the readlink command
  • man find - The manual page for the find command, useful for locating symbolic links
  • man symlink - The manual page describing the symlink system call


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)