What is Soft link and Hard link in Linux

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.

Jay

I love keeping up with the latest tech trends and emerging technologies like Linux, Azure, AWS, GCP, and other cutting-edge systems. With experience working with various technology tools and platforms, I enjoy sharing my knowledge through writing. I have a talent for simplifying complex technical concepts to make my articles accessible to all readers. Always looking for fresh ideas, I enjoy the challenge of presenting technical information in engaging ways. My ultimate aim is to help readers stay informed and empowered on their tech journeys.

Post a Comment

Previous Post Next Post

Contact Form