What is a Symbolic Link?
A symbolic link, sometimes called a symlink or soft link, is a file in Linux that points to other files or directories (folders) and represents their absolute or relative path. A symlink is similar to shortcuts in Windows and is useful when you need quick access to files or folders with long paths.
What Does a Symbolic Link Do?
A symbolic link points to the original file on your system but doesn't contain data in the target file. Consequently, removing the target file makes a symbolic link unusable, but removing the symlink does not affect the target file.
Benefits of Symbolic Links
Symbolic links are special files that point to the original file or directory in your system. Symlinks are beneficial when a file is frequently used. Symbolic links on Linux have advantages over hard links in these scenarios:
- When creating links for directories.
- When pointing to files or directories in another file system.
- They have a different inode and permissions from the original file or directory.
- They have smaller sizes.
What is the Difference Between a Hard Link and a Symbolic Link?
Hard links and symbolic links are two distinct methods for referring to files in the hard drive. They are part of the file system that organizes files and where they reside. A hard link is a synced copy of a file that directly refers to the inode of a file. Inodes are databases that describe the file or directory characteristics, such as metadata and the location on the hard drive. On the other hand, Symbolic links are shortcuts that refer directly to the file, which refers to the inode.
Follow this tutorial and learn how to create a symbolic link to files and directories.
- A Linux server.
- Access to a terminal with some command-line knowledge.
How to Create a Symbolic Link to a File
Create a symbolic link using the below syntax. This command creates the symlink in the current directory.
ln -s /path/to/file/to/be/linked symlink_name
Change symlink_name to your desired symlink name and /path/to/file to the actual file path. When you execute this command, you can access the original file path with the symlink name. For more details about the ln command, visit the ln man page or run man ln in your console.
The symbol used to signify a symbolic link is the (->) symbol. This symbol points to a file or its path if it is in a different location. Symbolic links have lrwxrwxrwx permissions, which are default permissions for symbolic links and cannot be changed. While lrwxrwxrwx permissions usually mean maximum allowable permissions in Linux, it is a dummy permission as symbolic links inherit the permissions of the file to which it links.
Symbolic links are also creatable using relative and absolute paths. Here are some sample outputs. In this example, sym1 is a symbolic link for a relative path and sym2 is for an absolute path.
[supendran@host symlinks]$ pwd /home/supendran/symlinks/ [supendran@host symlinks]$ ln -s file.txt sym1 [supendran@host symlinks]$ ln -s /home/supendran/symlinks/file.txt sym2 [supendran@host symlinks]$ ls -l total 4 -rw-rw-r-- 1 supendran supendran 0 July 22 18:12 file.txt lrwxrwxrwx 1 supendran supendran 9 July 22 18:23 sym1 -> file.txt lrwxrwxrwx 1 supendran supendran 30 July 22 18:24 sym2 -> /home/supendran/symlinks/file.txt
The cat command also outputs the contents of each file. For this example, they are the same.
[supendran@host symlinks]$ cat file.txt; cat sym1; cat sym2 This is my file.txt This is my file.txt This is my file.txt
How to Create a Symbolic Link to a Directory
Similar to creating a symbolic link to a file, use the following syntax to create a symlink to a directory. Change /path/to/folder to the directory path and symlink_name to the desired symlink name.
ln -s /path/to/folder/to/be/linked symlink_name
Here are example outputs after creating the symlink and listing the directory's contents.
[supendran@host symlinks]$ mkdir -p test/newdir [supendran@host symlinks]$ ln -s test/newdir/ newdir [supendran@host symlinks]$ ls -l total 4 lrwxrwxrwx 1 supendran supendran 18 July 22 18:28 newdir -> test/newdir drwxrwxr-x 3 supendran supendran 4096 July 22 18:29 test
How to Disable a Symbolic Link
There are two different ways to disable a symbolic link on Linux depending on the web server software you use. Add the following command to the .htaccess or the Apache configuration file to disable a symbolic link on Apache servers.
On NGINX servers, use their specific directive for disabling symlinks.
How to Remove a Symbolic Link
Use either the unlink or rm commands to remove a symbolic link. If the command executes correctly, it displays no output.
rm /path/to/symlink unlink /path/to/symlink
Notice the avoidance of the trailing slash (/) at the end of the symlink name.
Correct - /path/to/symlink vs Incorrect - /path/to/symlink/
It is safer to use the unlink command since it cannot remove directories; however, it can remove files. You may use the -i option to get prompted before removing the symlink using the rm command.
rm -i /path/to/symlink
Why Removing Symbolic Links is Important
Removing symlinks on Linux helps clean up the file system, thus making it coherent. It also helps avoid confusion in case the symlinks are obsolete or broken. While symlinks don’t take up much disk space, it is a good practice to remove unnecessary files.
Identify and Delete Broken Symlinks
You can find and remove a broken symlink under a given directory or sub-directories using the following command. It lists all the broken symlinks available for removal.
find /path/to/directory -xtype l
Here is the output.
Once you identify removable symlinks, use the unlink or rm commands to remove them.
Admins use a symlink for time saving measures when administering servers or personal systems. It allows you to access files or directories much faster. Use this tutorial to master the use of symbolic links.
Liquid Web offers many different hosting solutions, including VPS Hosting, Cloud Dedicated Servers, Dedicated Servers, and sophisticated options like VMware Private Cloud for enterprise workloads. Not sure which plan is best? Contact our sales team to help you get started today.
How to create symlinks?+
How to delete symlinks?+
What is the difference between symbolic links and hard links on Linux?+
Is it possible to update a symlink?+
- How to Install Apache Tomcat on Linux (AlmaLinux)
- ChatGPT Integration — How to Create a Plugin for ChatGPT
- Stable Diffusion AI Image Generator (SDXL) — Using the Web UI
- How to Install VMware Tools on Ubuntu: Step-by-Step Guide
- How to Install WordPress on Linux (AlmaLinux)
- What is CentOS? Everything You Need to Know
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.