Open In App

How to Symlink a File in Linux

Last Updated : 06 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Basis

Soft links

Hard links

Inode number

Soft links have different inode numbers.

Hard links have the same inode number. 

File creation

Soft links can be created for files and directories.

Hard links cannot be created for the directory.

Data

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.

File system

Soft links can be used across the file system.

Hard links cannot be used across the file system.

File permission

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

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

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.

ss3-(1)

Step 4: You can access the information using the link file.

cat gfgsym

`cat` command is used to see the content inside a file.

ss4

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

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

create a soft link

Step 3: We can easily access the link directory

ls -la | grep "symgfg"

dd3

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

dd7

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

ln -s -f gfgfile gfgsym1

Overwrite

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

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads