How to Use Symbolic Links on Ubuntu: Practical Guide for Linux Users

目次

1. Introduction

On Ubuntu and other Linux-based operating systems, the concept of “symbolic links” plays a crucial role. A symbolic link works much like a “shortcut” or “alias,” allowing you to create a reference to a file or directory at another location. If you’re familiar with computers, think of Windows shortcuts or Mac aliases—they work similarly to symbolic links in Linux.

However, symbolic links are more than just shortcuts—they’re deeply integrated into the Linux file system and are essential for software organization, development, and efficient system administration. For example, symbolic links are commonly used for managing configuration files, organizing directory structures, and even in large-scale production systems.

There is also a similar feature called “hard links,” but there are critical differences. A symbolic link is a “pointer” to another file or directory—if the target is moved or deleted, the link becomes a “broken link.” In contrast, a hard link points directly to the file’s data itself, so the data remains accessible even if the original file is deleted.

This article will explain, in a beginner-friendly way, how to create, manage, and make practical use of symbolic links on Ubuntu. Whether you’re just getting started or looking to learn advanced tips, this guide is for you.

侍エンジニア塾

2. Basic Commands for Symbolic Links on Ubuntu

To work with symbolic links in Ubuntu, you mainly use the ln command. In this section, we’ll cover the basics: creating, deleting, and modifying (overwriting) symbolic links.

2.1 Creating a Symbolic Link

Use the following command to create a symbolic link:

ln -s [target] [link_name]
  • -s stands for “symbolic.” If you omit this option, you’ll create a hard link instead, so be careful.
  • [target] is the file or directory you want to reference, and [link_name] is the name of the new link you are creating.

Example 1: Creating a symbolic link to a file

ln -s /home/user/data.txt ~/data-link.txt

Example 2: Creating a symbolic link to a directory

ln -s /var/log /home/user/log-link

About absolute vs. relative paths
Using an absolute path (like /home/user/…) ensures the link always points correctly, even if you move it. If you use a relative path, be aware of your current directory and the folder structure when creating the link.

2.2 Deleting a Symbolic Link

To delete a symbolic link, use the regular rm or unlink command, just like deleting a normal file.

  • rm [link_name]
    Removes the symbolic link itself. It does not delete the actual target file or directory.
  • unlink [link_name]
    This also deletes only the link, not the target.

Example:

rm ~/data-link.txt
unlink ~/data-link.txt

2.3 Modifying or Overwriting a Symbolic Link

If a symbolic link with the same name already exists and you want to overwrite it with a new target, add the -f (force) option:

Example:

ln -sfn /new/path/to/data.txt ~/data-link.txt

This lets you update the link’s target without having to delete it first.

3. Common Mistakes for Beginners

Here are common pitfalls beginners face when using symbolic links on Ubuntu. Knowing these in advance can help prevent issues.

3.1 Be Careful with the Second Argument (Link Name)

If you specify an existing directory as the second argument (link name) in ln -s [target] [link_name], a link will be created inside that directory with the same name as the original file or folder.

Example:

ln -s /home/user/data.txt /tmp/

This creates a link named data.txt inside the /tmp/ directory.
If you don’t explicitly set the link name, you might create links in unexpected places with unexpected names.

3.2 Watch for Trailing Slashes in Directory Names

If you accidentally include or omit a trailing slash when creating or deleting a directory link, you might end up acting on the real directory contents instead of the link.

Example:

  • Correct way to delete a link
  rm mydir-link
  • Incorrect way (with trailing slash)
  rm mydir-link/

In this case, rm mydir-link/ might delete the actual contents of the target directory!
When deleting a link, never add a slash at the end.

3.3 Understanding Broken Links

If the target of a symbolic link is deleted or moved, the link becomes a “broken link.”
Trying to access it will result in an error like “No such file or directory.”

How to check for broken links:

ls -l

Broken links usually display a non-existent path after the “→” symbol, often highlighted in red or another color in the terminal.

3.4 Permissions and Privileges

To create a link in privileged directories (like /usr/local/bin or /etc), you’ll need sudo rights.
If you try without permission, you’ll see a “Permission denied” error.
Always check if you have the necessary permissions before creating or deleting links.

4. Practical Use Cases for Ubuntu

Symbolic links aren’t just convenient—they’re essential for real-world workflows. Here are some common use cases on Ubuntu.

4.1 Placing a Shell Script Shortcut on Your Desktop

If you save a custom shell script in a system directory like /usr/local/bin, running it by full path every time is inconvenient.
Create a symbolic link on your desktop or preferred folder for quick access with a double-click or shortcut.

Example:

ln -s /usr/local/bin/myscript.sh ~/Desktop/myscript.sh

This allows you to run the script directly from the desktop.

4.2 Using a RAM Disk to Protect Your SSD

Saving temporary or frequently changed cache files directly on your SSD can reduce its lifespan.
By saving them on a RAM disk (like /tmp or /dev/shm) and using symbolic links to redirect, you can minimize SSD wear.

Example:

ln -s /dev/shm/cache /home/user/.cache

This stores app cache files in RAM, reducing SSD write cycles.

4.3 Sharing Configuration Files Across Multiple Environments

If you want to use the same config file in multiple projects or for different users, you can manage them centrally with symbolic links instead of copying files.

Example:

ln -s /etc/myconfig.conf ~/project1/myconfig.conf

This improves maintenance and keeps configs consistent across projects.

4.4 Organizing Large Data Sets with Virtual Directories

If you have files scattered across multiple drives or folders, you can use symbolic links to create a virtual “collection” directory without physically moving the files.

Example:

ln -s /mnt/dataA/image01.jpg ~/all-images/image01.jpg
ln -s /mnt/dataB/image02.jpg ~/all-images/image02.jpg

5. Advanced Techniques

Here are some advanced tips for making the most of symbolic links in your daily workflow or development projects.

5.1 Linking to Network Shared Directories

If you use multiple PCs at home or work, you often mount network shared directories (like /mnt/shared).
Creating symbolic links from your local work directories to network shares lets you access files as if they were in the same place.

Example:

ln -s /mnt/shared/documents ~/shared-documents

5.2 Organizing Config Directories in Web Servers and Development Environments

On web servers like Apache or Nginx, you may need to manage shared config files or directories across multiple sites.
Symbolic links offer flexible ways to organize and reuse config files.

Example:

ln -s /etc/nginx/sites-available/common.conf /etc/nginx/sites-enabled/common.conf

 

5.3 Version Management & Switching Between Multiple Versions

Symbolic links make it easy to switch between different versions of applications or libraries. For example, if you have /opt/myapp linked to the latest version, you can change the link to point to another version as needed.

Example:

ln -sfn /opt/myapp-v2.0 /opt/myapp

5.4 Managing dotfiles & Development Environments Centrally

Developers often manage dotfiles (like .bashrc or .vimrc) on GitHub and reuse them across multiple PCs using symbolic links.
For example, you can link to a cloned repository’s config files to unify environments on different computers.

Example:

ln -s ~/dotfiles/.vimrc ~/.vimrc

6. Symbolic Links and SEO

While symbolic links are mainly a file system feature, they can also impact SEO (Search Engine Optimization) when used with web servers or content management systems like WordPress. Here are some practical considerations for website operators and WordPress users.

6.1 Optimizing Web Directory Structures

Symbolic links let you reorganize your web server’s file structure without moving physical files, making it easier to change URL paths or resource locations.
For example, you can group large media files (images, videos, documents) in separate storage and create symbolic links inside your public web directory to make management and scaling easier.

Example:

ln -s /data/large-files /var/www/html/files

6.2 Use Cases with WordPress

In WordPress, it’s common to use symbolic links to map the uploads directory (where images and attachments are saved) to external or cloud storage.
This can save server space, make backups easier, and improve flexibility for multisite setups.

Example:

ln -s /mnt/external/uploads /var/www/html/wp-content/uploads

However, be careful with permissions and web server settings (like the FollowSymLinks option).
Also, search engines like Google can only crawl resources accessible via HTTP. Whether symbolic links impact SEO depends on your web server’s settings, so always consider directory structure and server configuration from the planning stage.

6.3 Crawl Optimization by Organizing Directories

Too many unnecessary directories or broken links can waste crawler resources and potentially lower your site’s ranking.
Regularly check symbolic links on your site using ls -l or link checker tools, and remove any unused or broken links to help improve SEO.

7. Summary (Checklist Format)

This article covered everything from the basics to advanced use cases and tips for using symbolic links on Ubuntu. Here’s a handy checklist to help you in practice.

Basic Symbolic Link Checklist

  • Define your purpose
    — Decide which files or directories you want to link and why.
  • Use the correct creation command
    — The basic form is ln -s [target] [link_name]. Pay attention to whether you use absolute or relative paths.
  • Safely delete or update links
    — Use rm [link_name] or unlink [link_name] to remove links only.
    — Use ln -sfn [target] [link_name] for safe overwriting.
  • Avoid common beginner mistakes
    — Double-check trailing slashes, link name specification, and required permissions.
  • Leverage real-world use cases for flexibility
    — Centralize config files, share directories, and optimize web server structures as needed.
  • Regularly check and clean up broken or unused links
    — Use ls -l or link checkers to maintain a tidy file system.

Advanced Tips

  • Symbolic links can streamline operations, development workflows, and optimize storage.
  • They’re useful not only for servers and dev environments but also for daily file management and backup strategies.

8. Q&A (FAQ)

Q1. What happens if I delete the target of a symbolic link on Ubuntu?

A.
The link itself remains, but it becomes a “broken link” if the target is missing. In ls output, the target path appears in red (or colored). Trying to access the link results in an error. Remove unused links with rm or unlink.

Q2. Can symbolic links be used for both files and directories?

A.
Yes, they work for both. Use ln -s for files or directories. This is especially useful for centralized config management or sharing directories between environments.

Q3. How do I change the target of an existing symbolic link?

A.
You can delete and recreate the link, or just use ln -sfn [new_target] [link_name] to overwrite it in one step.

Q4. Is it safe to use symbolic links on a WordPress site?

A.
Usually yes, for example when mapping uploads or theme/plugin directories to external storage. This can improve flexibility and backup efficiency. However, always test in a staging environment first, as some servers or permission setups may block symlinks.

Q5. Do I need sudo to create or delete symbolic links?

A.
It depends on the permissions of the target directory. You don’t need special rights in your home directory, but system directories (like /usr/local/bin or /etc) require sudo. If you get errors, check your permissions and try again.

Q6. What’s the difference between symbolic and hard links?

A.
Symbolic links are like “nicknames” or “references”—if the target is deleted or moved, the link breaks. Hard links point to the file data itself, so the contents remain accessible even if the original is deleted. Use each appropriately for your needs.