Create Symbolic Links in Linux: Everything You Need to Know

Download Article
Learn how to make shortcuts to files and directories in any version of Linux
Download Article

In Linux, you can create symbolic links (symlinks) to point to other files and directories. These symlinks act as shortcuts, similar to icons on your desktop. This guide will teach you how to symlink in Linux, both to files and directories, and help you view and manage your symlinks.

Things You Should Know

  • To create a symlink to a file, use: ln -s <source_file> <link_name>
  • To symlink a directory, use: ln -s <source_directory> <link_name>
  • To see which file or directory a symlink points to, use: readlink <link_name>
Section 1 of 7:

Symlink to a File

Download Article
  1. How.com.vn English: To make a symlink to a file, use ln -s source_file link_name.
    Replace "source_file" with the full path to the file you want to link to, and "link_name" with the label you'd like to give it.
    • For example, let's say you want to add a link to /etc/httpd/conf/httpd.conf to the current directory so you can edit it without typing the full path. In this example, we'll call the link "apacheconf." To do this, run the command ln -s /etc/httpd/conf/httpd.conf apacheconf.
      • You can omit the "link_name" to keep the same name as the original file. For example, if you enter ln -s /etc/httpd/conf/httpd.conf, this creates a symlink called "httpd.conf" in the current directory.
  2. Advertisement
Section 2 of 7:

Symlink to a Directory

Download Article
  1. How.com.vn English: To create a symbolic link to a directory, use ln -s source_directory link_name.
    The command is the same as creating a symlink to a file.
    • For example, let's say you want to add a link to /var/www/html to your home directory called "website." If you're in your home directory, you'd use ln -s /var/www/html website.
    • You can also specify a full path to the link destination, e.g., ln -s /var/www/html /home/myusername/website.
    • Any permissions you apply to a directory's symlink will apply to that directory.
Section 3 of 7:

See Where a Symlink Points

Download Article
  1. How.com.vn English: Use ls -l to view all symlinks in a directory and their destinations.
    For example, if there is a symlink called "log" in the current directory that points to /var/log, you'll see log -> /var/log/ in the list of files and directories.
    • You can also use readlink link_name to see where a specific symlink points.
  2. Advertisement
Section 4 of 7:

Overwrite a Symlink

Download Article
  1. How.com.vn English: Use ln -sf source_file/directory link_name to change a symlink's destination.
    • For example, let's say you have a symlink called "website" in the current directory that points to /var/www/html but should be pointing to /var/www/html2. If you used ls -n /var/www/html2 website, you'd get an error because the symlink already exists. Instead, use ls -sf /var/www/html2 website to overwrite the existing symlink.
Section 5 of 7:

Remove a Symlink

Download Article
  1. How.com.vn English: There are two ways to delete a symlink.
    • The first is to use rm, just as you would when deleting any other file. Using rm link_name only deletes the symlink—it will not delete the target file or directory.
    • You can also use unlink link_name to delete a symlink. This command also deletes the symlink without harming the original file.
  2. Advertisement
Section 6 of 7:

Find All Symlinks in a Directory

Download Article
  1. How.com.vn English: Use find directory_path -type l to see all symlinks in a directory.
    This command is recursive, so it will display all symlinks in the specified directory as well as its subdirectories.
    • If there are symlinks that lead to other symlinks, you can view the entire chain using namei link_name.
Section 7 of 7:

Find Broken Symlinks

Download Article
  1. How.com.vn English: Use find directory_path -xtype l to view all symlinks without targets.
    For example, to check for symlinks that don't point anywhere in /sbin, you'd use find /sbin/ -xtype l.
  2. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • If you omit the -s option in ln -s, you'll create a hard link instead. A hard link creates a mirror copy of the target file or directory instead of an alias.[1]
      • In most Linux graphical file managers, symlinks are marked with arrows.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn English: Luigi Oppido
      Written by:
      Computer & Tech Specialist
      This article was written by Luigi Oppido and by How.com.vn staff writer, Nicole Levine, MFA. Luigi Oppido is the Owner and Operator of Pleasure Point Computers in Santa Cruz, California. Luigi has over 25 years of experience in general computer repair, data recovery, virus removal, and upgrades. He is also the host of the Computer Man Show! broadcasted on KSQD covering central California for over two years. This article has been viewed 3,824 times.
      How helpful is this?
      Co-authors: 3
      Updated: December 31, 2023
      Views: 3,824
      Categories: Linux | File Manipulation
      Thanks to all authors for creating a page that has been read 3,824 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement