How to Symlink a File in Linux
Last Updated :
06 Oct, 2023
In Linux/UNIX, a symbolic link or soft link, also referred to as a symlink, is a useful tool for linking files or directories from different locations. Similar to a pointer in C programming, a symlink directs to the original file from an alternate location. The creation of symbolic links is facilitated by the ln command. However, it is important to note that a symlink will only function if the original file exists on the system. If the original file is accidentally deleted, the symlink file becomes unusable.
Advantages of Symlink
Symlink has many more advantages making it more reliable and efficient when it comes to usage.
- It is more efficient in linking files across the file system.
- Symlink has a special case that it can also create a link file of a directory.
- It creates multiple access points for a file without having original access copies of a file.
- Kernel checks the filename of a link file and directly goes to the original file like if kernel traverses the namespace for the user.
Disadvantages of Symlink
Symlink has some disadvantages when it comes to usage.
- The most disadvantage of a symlink is that it does not directly link to a file.
- There is no use of the symlink file if the original file gets deleted or changed.
- The possibility of symlink can be dead or dangling after creating multiple files.
Soft links vs Hard links
Soft links have different inode numbers.
|
Hard links have the same inode number.
|
Soft links can be created for files and directories.
|
Hard links cannot be created for the directory.
|
Soft links can be only used until the original files and directories are present.
|
Hard links can be used after the deletion of the file.
|
Soft links can be used across the file system.
|
Hard links cannot be used across the file system.
|
Original file permission (-rw-r–r–) and Link file permission (lrwxrwxrwx) are different in soft links.
|
Both files have the same permissions in Hard links.
|
How to Symlink a file in Linux?
Step 1: To symlink a file, first, we need to create a file named “gfgfile”
touch gfgfile
`touch` command is used to create a file.

touch
Step 2: For creating a symlink file, we can use the command as:
ln -s [original file] [symbolic link file]
- ln: make a link between files.
- -s: create a symbolic link of a file instead of a hard link.

ln -s
Step 3: As you can see, ‘gfgsym’ indicates an original file location.
ls -la
`ls` command is used to list all the file and directories.
.webp)
Step 4: You can access the information using the link file.
cat gfgsym
`cat` command is used to see the content inside a file.

How to symlink a directory in Linux?
Step 1: To symlink a directory, first, we need to create a directory using the mkdir command.
mkdir gfg

create directory
Step 2: We create a soft link to the directory using the ln -s command
ln -s gfg symgfg

create a soft link
Step 3: We can easily access the link directory
ls -la | grep "symgfg"

Force Overwrite Symbolic Links
Step 1: If we try to create a symbolic link that is already present, then it will display an error:
ln -s gfgfile gfgsym

Step 2: To overwrite symbolic links we can use the option -f or –force
ln -s -f gfgfile gfgsym1

Overwrite
How to remove the symlink file?
Step 1: To remove or unlink a symlink file, you can use the command rm or unlink
rm symlink_filename [or]
unlink symlink_filename [or]
rm symgfg

remove
Conclusion
In this article we discussed symlinks which is invaluable tools in Linux/UNIX for linking files and directories across different locations, functioning like pointers in C programming. However, symlinks are dependent on the existence of the original file, becoming unusable if it is accidentally deleted. Despite this limitation, symlinks offer advantages such as efficient linking, the ability to create links for directories, and multiple access points for files. Understanding their capabilities and limitations can greatly enhance file and directory management in the Linux/UNIX environment.
Similar Reads
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
How to Create File in Linux
Today, we're going to learn about something really important â how to create files in Linux. It's like creating a fresh piece of digital paper to write or store things. We'll explore different ways to do this using simple commands. Whether you're just starting out or have been using Linux for a bit,
7 min read
How to Rename a Folder in Linux
Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
12 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different
7 min read
How to Set File Permissions in Linux?
Ever worried about who can access, modify, or execute your critical files on a Linux system? File permissions are the backbone of Linux security, ensuring that only authorized users and processes interact with your data. In this guide, youâll learn how to master Linux file permissions using commands
10 min read
How to List Open Files in Linux | lsof Command
In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, w
7 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting o
8 min read
How to Download File on Linux Terminal
Downloading file on Linux is a basic operation, and the right command-line tool makes a huge impact in terms of speed, efficacy, and customization. While the graphical interface for simple downloads, CLI-based programs like wget, curl, axel, and aria2 come with features of parallel downloading, auth
7 min read
How to Get Last Modified Date of File in Linux?
Here we are going to see how to get the last modified date of the file in Linux, sometimes we may require timestamps of the file and apart from this it also ensures that we have the latest version of that file. It can be done in four ways: Using Stat command.Using date command.Using ls -l command.Us
1 min read