PDF download Download Article
Learn how to make shortcuts to files and directories in any version of Linux
PDF download 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

PDF download Download Article
  1. 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

PDF download Download Article
  1. 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

PDF download Download Article
  1. 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

PDF download Download Article
    • 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

PDF download Download Article
    • 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.
  1. Advertisement
Section 6 of 7:

Find All Symlinks in a Directory

PDF download Download Article
  1. 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

PDF download Download Article
  1. 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
      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
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 14,001 times.

      Is this article up to date?

      Advertisement