Home> System Tutorial> LINUX> body text

Examples to explain the creation of hard links and soft links in Linux systems

PHPz
Release: 2024-02-12 12:48:27
forward
522 people have browsed it

Linux links are divided into two types, one is called a hard link and the other is called a symbolic link. By default, the ln command generates hard links. The difference between hard links and soft links fundamentally starts with the inode node. The following are examples of creating hard links and soft links in Linux systems to see the differences between the two types of links in Linux.

Examples to explain the creation of hard links and soft links in Linux systems

First of all, we must understand that in the Linux system, the kernel allocates an Inode (index node) to each newly created file, and each file has a unique inode number. File attributes are stored in the index node. When accessing the file, the index node is copied to the memory, thereby achieving fast access to the file.

A link is a method of establishing a connection between a shared file and several directory entries of the user who accesses it. Linux includes two types of links: Hard Link and Soft Link. Soft Link is also called Symbolic link.

Hard link

To put it bluntly, a hard link is a pointer pointing to the file index node. The system does not reallocate the inode for it. You can use the :ln command to create a hard link. grammar:

ln [options] existingfile newfile ln[options] existingfile-list directory
Copy after login

Usage: First: Create a hard link for "existingfile" and the file name is "newfile". Second: In the "directory" directory, create a hard link with the same name for all files contained in "existingfile-list". Commonly used optional [options] -f creates a link regardless of whether "newfile" exists or not. -n If "newfile" already exists, no link will be created. The example is as follows:

$ ls –il 13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1 13059 -rwx - - - - - - 1 longcheng longcheng 57 8月 5 16:40 file2 $ ln file2 file2hard $ ls –il 13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard
Copy after login

Notice:
Before creating the link, the number of links displayed by file1 is 1
After creating the link, (1) the number of links for file1 and file1hard becomes 2; (2) the inode numbers of file1 and file1hard are the same; (3) the file sizes displayed by file1 and file1hard are also the same.
It can be seen that the result of the ln command operation is: file1 and file1hard are two names of the same file. They have the same index node number and file attributes. Creating a hard link to file file1 means that the file index node of file1 is in the current directory. Create a new pointer.

Examples to explain the creation of hard links and soft links in Linux systems

The number of links is reduced by one at the same time. Only when all pointers pointing to the file content, that is, the number of links is reduced to 0, will the kernel delete the file content from the disk. The current directory logical structure: (Sorry, the picture is not shown).

You can also create hard links to files in different directories but in the same file system. Assume that file1 and file2 are in the directory /home/longcheng/dir1. The following command creates a hard link to file2 in /home/longcheng.

ln file2 /home/longcheng/file2hard
Copy after login

The following program is to create hard links for all files in the dir1 directory in the directory dir2

$mkdir dir2 $ln /home/longcheng/dir1/* /home/longcheng/dir2
Copy after login

If ln -f existingfile newfile is used, if newfile already exists, no matter what file the original newfile is, only the current user has write permission to it, and newfile will become a hard link file of exitngfile.

Although hard links save space and are also the traditional way of integrating file systems in Linux systems, there are some shortcomings:
(1) Links cannot be established between files in different file systems
(2) Only super users can create hard links for directories. Although many trees say that root users can create, the author discovered during the learning process that even root users cannot create. My system is Redhat, and I have tried kernel 2.4 and 2.6. I don’t know if it is possible in other systems.

Soft link (symbolic link)

Soft links overcome the shortcomings of hard links and have no file system restrictions. Any user can create a symbolic link pointing to a directory. As a result, it is now more widely used, has greater flexibility, and can even link files across different machines and different networks.

To create a soft link, just add the option -s after ln. The example is as follows:

$ ls -il 13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard $ln –s file1 file1soft $ls -il 13058 -rwx - - - - - - 1 longcheng longcheng 48 8月 5 16:38 file1 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2 13059 -rwx - - - - - - 2 longcheng longcheng 57 8月 5 16:40 file2hard 13061 lrwxrwxrwx 1 longcheng longcheng 5 8月 5 16:58 file1soft->file1
Copy after login
Examples to explain the creation of hard links and soft links in Linux systems

It can be seen from the above link results that the difference between soft links and hard links is not only in concept, but also in implementation.

the difference:
The hard link original file & linked file share a common inode number, indicating that they are the same file, while the soft link original file & linked file have different inode numbers, indicating that they are two different files.
In the file attributes, the soft link clearly states that it is a link file, but the hard link does not, because in essence, the hard link file and the original file have a completely equal relationship.
The number of links is different, and the number of soft links will not increase; the file size is different, and the size displayed by the hard link file is the same as the original file.

In short, establishing a soft link is creating a new file. When a linked file is accessed, the system will find that it is a linked file, and it will read the linked file to find the file that is actually to be accessed. Of course, soft links also have disadvantages that hard links do not have, because the link file contains the path information of the original file, so when the original file is moved from one directory to another directory and the link file is accessed again, the system will not be able to find it, while the hard link will Without this flaw, you can move it however you want; it also requires the system to allocate additional space for creating new index nodes and saving the path of the original file.

The above is the detailed content of Examples to explain the creation of hard links and soft links in Linux systems. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!