To Create a Symlink for a File or Directory in Linux, First, we need to understand what Symlink is and how it works.
Table of Contents
- What is a Symlink?
- Create a Symlink for a File in Linux:
- Create Symlink for a Directory in Linux:
- Force Overwriting of Existing Symlinks:
- How to Remove a Symlink:
What is a Symlink?
Symlinks (symbolic links) are special types of files in Linux/Unix like systems, these files act as a pointer or reference (shortcut) to another file or directory. Thus, symlinks allow users to access a file or directory from a different location without duplicating the original content. Symlink works the same way as shortcuts in Windows, as they both depend on the existence of the original file, such that if the original file is deleted the symlink becomes invalid or broken. A symlink can point to files or directories located anywhere on the file system, even across partitions or network file systems.
How do Symlinks work?
- A symlink contains a reference to the path of another file or directory, called the target file.
- When the user accesses a symlink, the operating system redirects operations to the target file or directory.
Advantages of Symlinks
- Cross-File System Efficiency: Symlinks provide an efficient way to link files across different file systems.
- Directory Linking: Symlinks can link to directories, a feature not supported by hard links.
- Multiple Access Points: Symlinks allow multiple access points to a file without duplicating it. Since they do not duplicate content, they save storage space.
- Direct Kernel Access: The kernel directly accesses the original file when a symlink is used, ensuring seamless navigation.
Disadvantages of Symlinks
- Dependency on Original Files: If the original file is deleted or moved, the symlink becomes unusable.
- Not a Direct Link: Unlike hard links, symlinks do not link directly to the file data but reference the file name.
- Increased Access Time: Accessing a symlink introduces an additional lookup step to find the target.
- Circular References: Improper use of symlinks can result in circular dependencies that confuse software or users.
Soft Links(Symlinks) v/s Hard Links:
Aspect | Symlinks | Hard Links |
Path or Data Reference | Points to the file’s path. | Points to the file’s data (inode). |
Cross-File System | Supported. | Not supported. |
Directories | Can link to directories. | Cannot link to directories. |
Broken Links | Becomes invalid if the target is removed. | Remains functional unless all links are removed. |
Inode Sharing | Unique inode. | Same inode as the original file. |
Create a Symlink for a File in Linux:
Step 1: Create a File
Use the command below to create a new file:
$> touch [FILENAME]
The touch command creates an empty file.

Step 2: Create a Symlink
The ln
command on Linux is used to create a link between files or directories in Linux.
$> ln [option] [original file] [link file]
Original file: The original file or directory to which a link is to be created.
link file: The name of the new link file. If not specified, the link file will be created in the current directory with the same name as the target.
Use the command below to create a symbolic link:
$> ln -s [original file] [symbolic link file]
-s
: Specifies that it is a symbolic link.

Step 3: Verify the Symlink
Use the command below to check the symlink:
$> ls -l -a
-l
: Long listing format, provides detailed information about each file/directory.
-a
: Lists all files, including hidden files.

Step 4: Accessing the Symlink
Users can access the symlink similarly to the original file, such as:
$> cat [symlink file name]
The cat command will display the content of the symlinked file.
Create Symlink for a Directory in Linux:
Step 1: Create a Directory
Use the command below to create a directory:
$> mkdir [DIRECTORYNAME]
The mkdir
the command creates an empty directory.

Step 2: Create a Symlink to the Directory
Use the command below to create a symbolic link:
$> ln -s [original directory] [symbolic link directory]

Step 3: Verify the Symlink
Use the ls
command to confirm the symlink:
$> ls -l -a

Force Overwriting of Existing Symlinks:
If a symlink already exists and the user tries to create another one with the same name, an error occurs:
$> ln -s [original file] [existing symlink]

Thus, to overwrite an existing symlink file use -f
or --force
Option.
$> ln -s -f [original file] [existing symlink]

How to Remove a Symlink:
To delete or unlink a symlink, use either the rm
or unlink
command:
$> rm [symlink file]
$> unlink [symlink file]

Creating Symlinks (Symbolic links) for files and directories in Linux/UNIX are powerful tools for managing files and directories across different locations. They act like pointers, offering efficient file linking, access to directories, and multiple file access points. However, they rely on the existence of the original file, which can make them fragile. By understanding their creation, usage, and limitations, you can effectively use symlinks for file and directory management in Linux/UNIX environments.