In a Linux filesystem, it is incredibly common to need access to the same file or directory from two completely different locations. For example, a web server might expect a configuration file to be located at /etc/nginx/sites-enabled/, but as a developer, you want to keep that file stored in your personal ~/projects/website/ folder so you can edit it easily without using sudo. Copying the file twice is a terrible idea, as you would have to remember to update both copies every time you make a change. The solution is to create a “Symbolic Link” (also known as a symlink or soft link) using the Linux ln command.
What is a Symbolic Link?
Think of a symbolic link exactly like a “Shortcut” in Windows or an “Alias” in macOS. It is a tiny, empty file that simply acts as a signpost, pointing the operating system to the actual, physical location of the real data. If a program tries to read the symlink, the Linux kernel transparently redirects it to the real file. If you edit the real file, the symlink automatically points to the updated data.
How to Create a Symbolic Link
The syntax for the ln command can be confusing for beginners because it requires both a Target (the original, real file) and a Link Name (the fake shortcut you are creating). It is crucial to remember the order: Original first, Shortcut second.
To create a symbolic link, you must use the -s (symbolic) flag.
- Open your Linux terminal.
- Type the command using absolute paths to avoid broken links:
ln -s /original/real/path/database_config.yaml /fake/shortcut/path/config.yaml - Press Enter.
If the command succeeds, it executes silently. If you run ls -l in the /fake/shortcut/path/ directory, you will see a new entry that looks like this:
lrwxrwxrwx 1 user user 38 Oct 26 10:00 config.yaml -> /original/real/path/database_config.yaml
The l at the very beginning of the permissions string and the -> arrow definitively prove that this is a shortcut, not a real file.
Linking Entire Directories
The ln -s command works flawlessly on directories as well as individual files. If you have a massive folder of media assets and want to make it accessible to your web server without moving the data, you can link the entire folder.
ln -s /mnt/storage_drive/media_assets/ /var/www/html/assets
Now, if a user visits yourwebsite.com/assets/video.mp4, the web server will seamlessly reach into your storage drive to serve the file.
The Danger of Broken Links
Because a symbolic link is merely a signpost, it has absolutely no control over the original file. If you delete the original file (database_config.yaml), the symlink (config.yaml) will not be deleted. It will remain on your hard drive, but it will become a “broken” or “orphaned” link. If you try to open the broken link, the terminal will throw a No such file or directory error. When running ls -l, many terminal emulators will highlight broken links in flashing red text to warn you.
To remove a symlink, you simply delete it using the standard rm command (e.g., rm /fake/shortcut/path/config.yaml). This will delete the shortcut safely; it will not delete the original file it points to.