Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Create and Remove Symbolic Links in Linux

Make shortcuts to files and folders with ln -s, and remove them safely.

How-To Guides2 min read8 steps
  1. A symbolic link (symlink) is a shortcut that points to a file or folder. It is not a copy.
  2. You create it with ln -s.
  3. You remove it with unlink or rm.
  4. Deleting a symlink removes only the shortcut, not the original.

If the original is deleted or moved, the symlink breaks (it is "dangling").

ln -s target link_name
  • target is the file or folder that already exists.
  • link_name is the new shortcut.

Example, link a folder:

ln -s /media/music music

Example, link a file to another location:

ln -s /home/user/Documents/original.pdf ~/Desktop/original-link.pdf

Steps:

  1. Open the terminal.
  2. Run ln -s.
  3. Give the full path of the original file or folder first.
  4. Give the name (and location) of the new link second.

Use full (absolute) paths for the target, or the link may point to the wrong place when you move it.

ls -l music

The output starts with l and shows an arrow:

lrwxrwxrwx 1 user user 12 Jan 10 10:00 music -> /media/music

Find broken links in a folder:

find . -xtype l

With unlink

unlink music

With rm

rm music
Warning: Do not add a trailing slash when you remove a link to a folder. rm -r music/ can act on the contents of the target folder. Use rm music or unlink music.
ln -sfn /new/target link_name

-f overwrites an existing link and -n treats a link to a folder as a normal file.

Symbolic linkHard link
Commandln -sln
Points toA pathThe same data on disk
Works for foldersYesNo
Across file systemsYesNo
If the original is deletedThe link breaksThe data stays

Common uses

  • Enable an Nginx site: ln -s /etc/nginx/sites-available/site /etc/nginx/sites-enabled/.
  • Keep several versions of software and point current to the active one.
  • Put a short path for a deep folder.

Quick recap

  • ln -s target link creates it, unlink link removes it.