How to Create Symbolic Links in Ubuntu Linux

Symbolic links (symlinks) create lightweight pointers to files and directories, giving you fast shortcuts without duplicating data, similar to how Windows shortcuts reference programs without copying them. They streamline file management, surface configuration files where you need them, and keep projects tidy across disks and mount points. Combined with foundational tasks like moving files and directories, symlinks make it easy to reorganize complex trees without extra copies.

This guide covers creating symlinks with ln, choosing between relative and absolute targets, safely updating or removing existing links, mirroring a directory tree with symlinked files, and troubleshooting with readlink and find. The examples focus on practical Ubuntu usage that transfers well to other Debian-based systems.

What Are Symbolic Links in Linux?

Symbolic links act as pointers to files or directories rather than storing the actual data. Unlike hard links, which directly reference file data at the inode level (an inode is the filesystem record that tracks a file’s metadata), symlinks only reference the file path. If the original file is moved or deleted, the symlink becomes broken.

Key Benefits of Using Symbolic Links

  • Simplifies file access by creating quick shortcuts to deeply nested directories or frequently used files
  • Centralizes configuration management by linking configuration files instead of copying them across multiple locations
  • Works across different filesystems unlike hard links, symbolic links can point to files stored on different partitions or external storage devices
  • Improves script management by linking scripts or binaries to commonly accessed directories such as /usr/local/bin, allowing execution from any location

Create Symbolic Links on Ubuntu

The ln command creates symbolic links in Ubuntu. To ensure you create symbolic links rather than hard links, include the -s option with the command.

Basic Syntax

ln -s [target] [symlink_name]
  • [target] refers to the original file or directory the symlink will point to
  • [symlink_name] is the name of the symbolic link being created

Common ln Options Explained

The ln utility ships with the GNU coreutils package on Ubuntu and includes several helpful switches. Understanding what each option does prevents accidental overwrites and hard-to-trace mistakes.

OptionWhat it doesWhen to use it
-sCreates a symbolic link instead of a hard link.Always include this flag when you need a symlink.
-fRemoves an existing destination before creating the new link.Safe way to update links without manual cleanup.
-nTreats the destination as a normal file, even if it is a directory symlink.Prevents ln from following an existing directory symlink.
-TForces ln to treat the link name as a file, not a directory.Helpful when the destination path might already exist as a directory.
-vPrints verbose output as the link is created.Use during troubleshooting to confirm results.

Example: Creating a Symlink to a File

ln -s /etc/nginx/nginx.conf ~/nginx.conf

This creates a symlink in the home directory pointing to the actual Nginx configuration file. Instead of navigating to /etc/nginx/nginx.conf, you can access it quickly using ~/nginx.conf.

Verify a Newly Created Symlink

Confirm the link worked as expected before relying on it in scripts. A quick ls -l reveals the arrow (->) that shows the target, while readlink resolves the final destination.

ls -l ~/nginx.conf
readlink -f ~/nginx.conf

If the output does not point where you expect, recreate the link with the correct path or use the -f flag to replace it safely.

Creating Symbolic Links for Directories

Symlinks can also point to directories, making navigation simpler.

Example: Creating a Symlink to a Directory

ln -s /var/www/html ~/website

After creating this symlink, you can access the directory /var/www/html using ~/website, reducing the need for long paths.

Removing Symbolic Links

Since symbolic links do not contain actual data, deleting them does not affect the original file or directory.

Deleting a Symlink

rm /path/to/symlink

Alternatively, you can use unlink for removing individual symlinks.

unlink /path/to/symlink

This method is useful for ensuring only the link is removed without affecting actual files.

Update or Replace Existing Symbolic Links

Symlinks don’t support in-place edits; you replace the link with a new one. The most direct method is ln with -s (symbolic) and -f (force), which removes any existing link path before creating the new one.

ln -sf /new/path/file.txt /path/to/symlink

If you prefer an explicit two-step process, delete the old link and create a new one:

rm /path/to/symlink
ln -s /new/path/file.txt /path/to/symlink

Advanced Use Cases for Symbolic Links

Symbolic links are commonly used in system administration and software management. Here are some advanced use cases demonstrating their real-world benefits.

Linking Executables for Global Access

If you manually install software outside system directories, creating a symlink in /usr/local/bin/ allows you to run the program from any location. Because that directory is writable only by root on Ubuntu, prepend sudo when creating the link.

sudo ln -s /opt/customapp/custombinary /usr/local/bin/customapp

This allows you to run customapp from anywhere in the terminal rather than specifying the full path each time.

Ensure the target binary is executable; otherwise, the symlink won’t run. You can set the executable bit with chmod:

chmod +x /opt/customapp/custombinary

Organizing Dotfiles and Shared Configurations

Many developers track dotfiles in a Git repository and link them into place. Symlinks make it easy to keep version-controlled copies under ~/dotfiles while exposing them in the locations applications expect.

ln -sfn ~/dotfiles/.bashrc ~/.bashrc
ln -sfn ~/dotfiles/.config/nvim ~/.config/nvim

The -sfn combination forces an overwrite and prevents ln from traversing existing directory symlinks, which protects nested configuration trees from being merged unexpectedly.

Linking to Network Shares

Symlinks provide quick access to mounted network directories, saving time navigating long paths.

ln -s /mnt/server/share ~/network_drive

This allows instant access to the network share by using ~/network_drive.

Creating Relative Symbolic Links

Relative symlinks are useful when working with files that are likely to be moved together.

ln -s ../shared/resources project-resources

This ensures project-resources always points to ../shared/resources, even if the directory structure is moved.

Replicating a Directory Structure with Symbolic Links for Files

While the ln command doesn’t create a single “recursive” symlink that dynamically reflects all contents of a target directory through one link, you can use commands like find in combination with mkdir and ln to create a mirrored directory structure where symlinks represent all original files in the new structure. This approach proves useful for creating a linked copy of a complex directory tree.

For example, to replicate the structure of /original/directory under /symlink/directory, with all files in the replicated structure being symlinks to their originals:

cd /original/directory
# First, create the same directory structure in the target location
find . -type d -exec sh -c 'mkdir -p "/symlink/directory/$1"' _ {} \;
# Then, create symlinks for all files into the new structure
find . -type f -exec sh -c 'ln -s "$(pwd)/$1" "/symlink/directory/$1"' _ {} \;

This method first creates all necessary subdirectories in /symlink/directory and then populates them with symlinks pointing back to the files in /original/directory. The shell wrapper (sh -c) ensures paths with spaces or special characters receive correct handling. The symlinks use absolute paths to the original files, making them more robust if you move /symlink/directory (though the links would still break if you move /original/directory or files within it). You should ensure /symlink/directory exists before running these commands or that the user running them has permission to create it and its subdirectories.

Limitations and tips: this mirrors only regular files (existing symlinks inside the source tree are skipped); if a destination path already exists, ln will fail unless you deliberately use -f; absolute targets make the mirror portable when moving /symlink/directory but will break if you later move /original/directory. Creating correct relative symlinks is possible but requires computing a per-file relative path (for example, with realpath --relative-to). For background on using find -exec, see this guide.

Troubleshooting Symbolic Links

Why Is My Symlink Broken?

A broken symlink occurs when someone moves, renames, or deletes the target file or directory it points to. To find all broken symlinks within a specific directory and its subdirectories, use the following command:

find /path/to/check -xtype l

You can remove all such broken symlinks found recursively:

find /path/to/check -xtype l -delete

To audit broken links and see which targets they reference, list them with their stored paths before cleanup:

find /path/to/check -xtype l -printf '%p -> %l\n'

How Can I List All Symbolic Links in a Directory?

To display symbolic links located directly within the current directory (or a specified directory), use find with a shallow depth limit so hidden entries and locale-specific ls output remain visible:

find . -maxdepth 1 -type l -exec ls -l {} +

This lists every symlink in the current directory, including dotfiles, without tripping over color codes or locale differences.

If you need to find all symbolic links recursively within a directory and its subdirectories, a more comprehensive command is:

find /path/to/your_directory -type l

Replace /path/to/your_directory with the actual directory path. This command will list all files of type l (symlink) within that path.

How to Check a Symlink Target

To see where a symbolic link points, use the readlink command. This is especially useful for troubleshooting or verifying symlink targets:

readlink -f /path/to/symlink

This prints the absolute path of the file or directory the symlink references.

readlink -f resolves through chained links to the final target. If any element in the chain is missing, resolution stops at the deepest existing path, which helps identify where the break occurred.

For more details, see the official ln man page.

Note on Permissions

When creating symbolic links in system directories such as /usr/local/bin or /etc, you may need to use sudo to gain the necessary permissions. For example:

sudo ln -s /opt/customapp/custombinary /usr/local/bin/customapp

Always use caution when running commands with sudo.

On Linux, a symlink’s own permission bits are typically ignored; access checks evaluate the target’s ownership and mode. In practice, the link’s permissions rarely matter; the target’s permissions determine whether operations succeed.

Quoting Paths with Spaces

If your file or directory names contain spaces, enclose the paths in straight quotes to avoid errors. When referring to your home directory, use $HOME (which expands safely inside quotes) or leave the tilde unquoted. For example:

ln -s "/path/with spaces/original file.txt" "$HOME/link name.txt"

Relative vs. Absolute Symlinks

Symbolic links can use either absolute or relative paths for their targets.

Relative symlinks prove useful when you expect to move a project or directory tree as a unit (such as in version-controlled repositories), since the links remain valid as long as you preserve the relative structure. They specify the target path relative to the location of the symlink itself.

# Assume your current directory is /home/user/project
# The target file is /home/user/shared/config.txt
# Create a relative symlink in /home/user/project
ln -s ../shared/config.txt project_config

In this example, project_config in /home/user/project points to /home/user/project/../shared/config.txt, which resolves to /home/user/shared/config.txt. If you move the project and shared directories together to another location (e.g., /new/location/user/project and /new/location/user/shared), the relative link remains valid.

Absolute symlinks work best when the target location stays fixed or you share it across different locations, as they always point to the same absolute path regardless of where you move the symlink. They specify the full path from the root directory (/).

# The target file is always /etc/nginx/nginx.conf
# Create an absolute symlink in your home directory
ln -s /etc/nginx/nginx.conf ~/nginx.conf

In this case, ~/nginx.conf will always point directly to /etc/nginx/nginx.conf, regardless of your current location or if you move your home directory. Absolute links will break if the target file or directory’s absolute path changes.

Choose the type that best fits your workflow and how you expect to move or share your files.

Best Practices for Managing Symlinks

  • Prefer relative paths inside project directories so clones or backups stay portable.
  • Keep naming predictable: mirror the original filename to make broken links easier to diagnose.
  • Document custom links in provisioning scripts or README files to avoid surprises for collaborators.
  • Audit regularly with find -xtype l on shared servers (or print mappings with -printf '%p -> %l') so stale links do not accumulate.
  • Test with non-root accounts before relying on symlinks in production automation, especially when sudo is required.

Conclusion

Symlinks provide fast, flexible access to files and directories without duplication. With ln, you can create links, safely update or replace them, and choose relative or absolute targets for portability. Troubleshooting with readlink and find keeps targets accurate, while small practices such as predictable names, executable binaries in /usr/local/bin, and periodic audits help maintain a clean, reliable filesystem.

2 thoughts on “How to Create Symbolic Links in Ubuntu Linux”

Leave a Comment