How to Create Symbolic Links in Ubuntu?

This article provides a straightforward guide on how to create a symbolic link (symlink) between two directories in Ubuntu using the terminal. You will learn the correct command syntax, how to verify that your link was created successfully, and how to safely remove a symlink when it is no longer needed. Understanding this process allows you to map directory paths efficiently, saving disk space and streamlining your file management.

Understanding the ln Command

In Ubuntu, symbolic links are created using the ln command with the -s flag (which specifies that the link should be symbolic rather than a hard link). The basic syntax requires you to point to the existing target directory first, followed by the path where you want the new shortcut to live.

The standard syntax is:

ln -s /path/to/original_directory /path/to/shortcut_link

Step-by-Step Guide to Linking Directories

To successfully create a symlink between two directories, open your terminal and follow these steps:

  1. Identify the Paths: Determine the absolute or relative path of the existing directory (the source) and the path for the new link you want to create (the destination).
  2. Run the Command: Input the ln command with the source and destination paths. For example, to link a folder named “Project” in your Documents to your Desktop, you would run: ln -s /home/user/Documents/Project /home/user/Desktop/Project_Shortcut
  3. Handle Permissions: If you are creating a link within system directories outside of your home folder, you will need to prepend the command with sudo: sudo ln -s /var/www/html /home/user/mymedia

Once the command is executed, it is good practice to verify that the link was created correctly and points to the right location. You can do this by listing the directory contents with the long listing format command:

ls -l /path/to/shortcut_link

The output will display an arrow pointing from the shortcut to the original directory, looking similar to this: Project_Shortcut -> /home/user/Documents/Project.

If you ever need to remove the symbolic link, use the rm command followed by the path of the shortcut. Be careful not to include a trailing slash at the end of the shortcut name, as this can accidentally target the contents of the original folder instead of the link itself:

rm /path/to/shortcut_link