When navigating a complex Linux server, you often find yourself typing out the same long directory paths repeatedly. For example, if your web server files are buried at /var/www/html/mysite/public_html/assets, having to type that absolute path every time you want to edit a CSS file is exhausting.
Instead of constantly typing long paths or copying files to multiple locations, you can create a symbolic link (often called a symlink or soft link). A symbolic link acts exactly like a “shortcut” in Windows or an “alias” in macOS. It is a tiny placeholder file that points to the actual file or directory located elsewhere on the system. When you interact with the symlink, Ubuntu automatically redirects the action to the target.
This guide explains how to use the ln command in the Ubuntu terminal to create, verify, and safely delete persistent symbolic links.
The Anatomy of the ln Command
To create a symbolic link, you use the ln (link) command with the -s (symbolic) flag. If you forget the -s flag, Linux will attempt to create a “hard link,” which behaves completely differently and cannot link to directories.
The syntax for creating a symbolic link is always:
ln -s /path/to/the/actual/target /path/to/the/new/shortcut
Crucial Rule: Always use absolute paths (starting with / or ~) when defining the target. If you use relative paths, the symbolic link will instantly break if you ever move it to a different directory.
How to Create a Directory Symlink
Let’s solve the web server problem mentioned earlier. We want to create a shortcut in our home directory (~) called web_assets that points directly to the buried web server folder.
Open your terminal and run:
ln -s /var/www/html/mysite/public_html/assets ~/web_assets
The command executes silently. Now, whenever you type cd ~/web_assets, the terminal will instantly transport you to the deep /var/www/ directory, bypassing the need to type the full path.
How to Create a File Symlink
Symlinks are equally useful for individual files. For example, many applications expect configuration files to be located in specific, hidden system directories (like /etc/). Rather than editing the file in that hidden directory, you can create a symlink in your home folder for easy editing.
sudo ln -s /etc/nginx/sites-available/mysite.conf ~/mysite_config
Now, if you run nano ~/mysite_config, you are actually editing the live configuration file located in the /etc/nginx/ directory.
How to Verify a Symbolic Link
To verify that your symlink was created correctly and points to the right location, use the list (ls) command with the long format (-l) flag.
ls -l ~/web_assets
In the terminal output, you will see a small arrow (->) explicitly showing exactly where the shortcut points:
lrwxrwxrwx 1 user user 38 Sep 12 10:00 web_assets -> /var/www/html/mysite/public_html/assets
The l at the very beginning of the permissions block (lrwxrwxrwx) confirms that this object is a link, not a standard file or directory.
How to Safely Delete a Symbolic Link
If you no longer need the shortcut, you can delete it using the standard rm (remove) or unlink command.
rm ~/web_assets
Warning: When deleting a directory symlink using rm, never add a trailing slash to the end of the symlink name (e.g., never type rm ~/web_assets/). Adding the slash tells Linux to look inside the target directory and delete the actual contents, rather than deleting the shortcut itself. Always delete the symlink name exactly as it appears.
By mastering symbolic links, you can radically streamline your workflow in the Ubuntu terminal, keeping important files accessible without duplicating data or wasting time typing long paths.