Open In App

How to Symlink a File in Linux

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.



Disadvantages of Symlink

Symlink has some disadvantages when it comes to usage.

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

Step 2: For creating a symlink file, we can use the command as:

ln -s [original file] [symbolic link file]

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.

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.


Article Tags :