sodcalculator

10 Tech How-To

How to Create Symbolic Links (Symlinks) in Linux

Guide 10-43078 · Updated · Step by step

Symbolic links (symlinks) are special files that point to another file or directory, letting you access it from a different location without copying it. This guide shows you how to create, view, and remove symlinks using the ln command, with practical examples and troubleshooting tips.

What You’ll Need

  • A Linux system with a terminal (any distribution works).
  • Basic familiarity with the command line (cd, ls, pwd).
  • Write permissions on the directory where you want to create the symlink.

Step-by-Step

  1. Open a terminal. Use your system’s terminal emulator (e.g., GNOME Terminal, Konsole, or a TTY).
  2. Understand the basic syntax. The ln command creates links. For a symbolic link, use the -s option:
    ln -s target link_name

    Replace target with the path to the file or directory you want to link to, and link_name with the name of the symlink you want to create.

  3. Create a symlink to a file. For example, to create a symlink named mylink that points to /home/user/documents/report.txt:
    ln -s /home/user/documents/report.txt mylink

    This creates a symlink named mylink in your current directory. You can now access the file via mylink.

  4. Create a symlink to a directory. The same command works for directories:
    ln -s /var/www/html mywebsite

    Now mywebsite points to the /var/www/html directory.

  5. Use relative paths (optional). If you want the symlink to work even if the parent directories move, use a relative path for the target. For example, from within /home/user, create a link to documents/report.txt:
    ln -s documents/report.txt report-link

    This creates a symlink that is relative to the symlink’s location.

  6. Verify the symlink. Use ls -l to see the symlink and its target:
    ls -l mylink

    Output shows something like: mylink -> /home/user/documents/report.txt. The arrow indicates the target.

  7. Remove a symlink. Use rm to delete a symlink (do not use rm -r on a symlink to a directory; just rm is enough):
    rm mylink

Common Mistakes

  • Forgetting the -s flag: Without -s, ln creates a hard link, which behaves differently and cannot cross filesystems.
  • Using absolute vs. relative paths incorrectly: If you use a relative path, it is interpreted relative to the symlink’s location, not your current directory. This often causes broken links.
  • Putting a trailing slash on the target directory: ln -s /path/to/dir/ link may work, but it can cause issues with some commands. Generally, omit the trailing slash.
  • Overwriting existing files: If link_name already exists, ln will fail with an error. Use ln -sf to force overwrite (but be careful, as this deletes the existing file).

Tips

  • Use absolute paths for simplicity: If you’re unsure about relative paths, use absolute paths (starting with /) to avoid confusion.
  • Check the target exists: Before creating a symlink, verify the target path is correct with ls -l /path/to/target.
  • Use readlink to see the target: readlink mylink prints the path the symlink points to.
  • Find broken symlinks: Use find . -type l -xtype l to locate symlinks whose targets no longer exist.
  • Create a symlink in a different directory: You can specify a full path for the link name, e.g., ln -s /target /path/to/link.

Troubleshooting

  • Symlink is broken (target not found): This happens when the target path is incorrect or the target has moved. Use ls -l link to see the target, then check if it exists. Recreate the symlink with the correct path.
  • Permission denied: If you get “Permission denied” when creating a symlink, you don’t have write permission on the directory. Use sudo if you need to create it in a system directory, or change to a directory you own.
  • Symlink points to itself: If you create a symlink with the same name as the target in the same directory, you can create a loop. For example, ln -s file file creates a symlink that points to itself. Avoid this by using different names.
  • Command not found: If ln is not found, ensure your PATH is set correctly. Usually, ln is in /bin or /usr/bin.

Frequently Asked Questions

  1. What is the difference between a symbolic link and a hard link? A symbolic link is a separate file that points to a path. A hard link is an additional name for the same inode. Hard links cannot cross filesystems and cannot link to directories (usually). Symlinks are more flexible.
  2. Can I create a symlink to a symlink? Yes, you can. The new symlink will point to the target of the first symlink (or to the first symlink itself, depending on how you specify the target). It’s generally better to point to the real file.
  3. Do symlinks work across different filesystems? Yes, symlinks can point to any file or directory on any mounted filesystem, as long as the path is valid.
  4. How do I update a symlink to point to a new target? Simply remove the old symlink and create a new one with the same name: rm link then ln -s newtarget link. Or use ln -sfn newtarget link to force overwrite.
  5. Why do some symlinks show a red background in ls? This is often a color indicator for broken symlinks (target missing). Check the target path.
  6. Can I use symlinks in scripts? Yes, but be aware that some commands may follow symlinks automatically (e.g., cat) while others may not (e.g., find with certain options). Use -L or -P options to control behavior.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →