In Ubuntu Linux, managing file paths across complex server environments or software installations can be challenging. Often, a program expects a configuration file to be located in a specific directory (like /etc/), but your custom setup stores it elsewhere (like /opt/). To bridge this gap without duplicating files and wasting storage space, you can use symbolic links (symlinks).
What is a Symbolic Link?
A symbolic link is essentially a highly advanced shortcut. When you create a symlink, you create a tiny file that simply points to another file or directory elsewhere on the system. When an application attempts to read the symlink, the operating system seamlessly redirects it to the actual target file. If you edit the file via the symlink, you are directly editing the original file.
The Syntax of the ln Command
Symlinks are created using the ln command (which stands for “link”). To create a symbolic link rather than a hard link, you must use the -s flag.
The basic syntax is: ln -s [TARGET_FILE] [LINK_NAME]
- TARGET_FILE: The absolute path of the original, existing file you want to point to.
- LINK_NAME: The path and name of the shortcut you are creating.
How to Create a File Symlink
Let’s say you have a configuration file located at /home/user/custom-config.conf, but a system service expects it to be located at /etc/app/config.conf.
- Open your terminal window.
- Type the following command:
sudo ln -s /home/user/custom-config.conf /etc/app/config.conf - Press Enter and provide your administrator password.
The system will instantly create the config.conf shortcut in the /etc/app/ directory.
How to Verify a Symlink
If you want to check if a file is a symlink and see exactly where it points, you can use the list command with the long-format flag.
- Navigate to the directory containing the link:
cd /etc/app/ - Type the following command:
ls -l
In the output, you will see your file listed with an arrow pointing to its true location, looking like this: config.conf -> /home/user/custom-config.conf.
How to Delete a Symlink
Deleting a symbolic link is completely safe and does not delete the original target file.
- Open the terminal.
- Use the standard remove command:
rm /etc/app/config.conf
Only the shortcut is destroyed, leaving your original configuration file untouched in your home directory.