What is Soft link and Hard link in Linux

by Jay
Soft and Hard Link

What is a link file and how many types?

The link file is a shortcut file to the original file. Creating and removing (deleting) inks between
two files is known as managing links. There are two types of link files available in Linux.


(i)
Soft link

(ii)
Hard link

What is a soft link and how to create it?

A soft link is nothing but a shortcut file. If the original file is deleted, no use of the shortcut file. ie., we cannot access the original data by selecting the link file. Soft links can be applied to both directories and files. These files can be stored in any of the file systems. ie., the original file may be in one file system and the link file may be in another file system. If we edit any file, the link files are also updated automatically. When we create a soft link file, the permissions are full permissions. The soft link file and the original file inode no’s are different. The size of the soft link file is the same as the length of the original file name. The soft link can be created by

Syntax to create a soft link

# ln -s <original file or directory> <link file or directory with path>

Syntext to create a link file for the script and stored on root Desktop

# ln -s /root/script /root/Desktop/script  

What is a hard link and how to create it?

Hard link in nothing but a backup file. If the original file is deleted, there is no effect on the hard link file. ie., we can access the original file data even though the link file is deleted. Hard links can be applied on files only not on directories. Hard link files can be stored in the same file system. ie., original and hard link files both should be in the same file system not on different file systems. The inode no’s are the same for the original and hard link files. If the original is edited, the updations are applied to both the original and hard-link files. The size of the hard link file is the same as the size of the original file.

In Linux, we can create hard links using the ln command. A hard link is a reference to an existing file, and both the original file and the hard link refer to the same underlying data on the file system.

The syntax for creating a hard link is:

ln <source_file> <link_name> 

Here, <source_file> is the existing file to which you want to create a hard link, and <link_name> is the name of the hard link that you want to create.

For example, if we have a file named file1.txt and you want to create a hard link to it named file2.txt, you can use the following command:,

ln file1.txt file2.txt

This will create a hard link named file2.txt that points to the same data as file1.txt.

We can also create a hard link to a directory using the -d option with the ln command. For example:

ln -d /path/to/source/directory /path/to/link/directory

This will create a hard link to the directory specified by /path/to/source/directory, with the name /path/to/link/directory,. Note that creating hard links to directories can have unintended consequences, so it is generally not recommended.

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.