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 -sStep 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 linkStep 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
OverwriteHow 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
removeConclusion
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 In UNIX-based operating systems like Linux and macOS, `mv` stands for "move". The mv command is a UNIX command for renaming and moving files and directories within a filesystem. Although desktop operating systems have graphical user interfaces for file operations, calling mv in the terminal, usually
7 min read