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

Source: Internet
Author: User

Guide There are two types of Linux links, one called hard links, and the other called Symbolic links (symbolic link). By default, the LN command produces a hard link. The difference between the hard link and the soft link fundamentally starts with the Inode node, and the following example explains the creation of hard links and soft links in Linux systems, to actually look at the differences between the two ways of linking in Linux.

The first thing to figure out is that in a Linux system, the kernel allocates an Inode (index node) for each newly created file, each with a unique inode number. The file attributes are stored in the index node, and the index nodes are copied to the file when they are accessed, enabling quick access to the file.

A link is a way to establish a connection between a shared file and a number of directory entries for the user who accesses it. There are two types of links in Linux: Hard links and soft links (Soft link), and soft links called Symbolic links (symbolic link).

Hard Links

Hard links plainly is a pointer to a file index node, and the system does not redistribute the inode for it. You can use the: ln command to create a hard link. Grammar:

ln [options] existingfile newfileln[options] Existingfile-list directory

Usage: First: Create a hard link for "Existingfile" with the file name "NewFile". The second type: in the "directory" directory, create a hard link with the same name for all files contained in "Existingfile-list". Common optional [options]–f the link is created regardless of whether the newfile exists or not. -N If "NewFile" already exists, no links are created, as shown in the following example:

$ ls–il13058-rwx------1 Longcheng Longcheng 48 August 5 16:38 file113059-rwx-------1 Longcheng Longcheng 57 August 5 16:40 File2
$ ln file2 file2hard$ ls–il13058-rwx------1 Longcheng Longcheng 48 August 5 16:38 file113059-rwx-------2 LONGC Heng Longcheng 57 August 5 16:40 file213059-rwx------2 Longcheng Longcheng 57 August 5 16:40 File2hard
Attention:
The number of links displayed by File1 is 1 before the link is created.
After creating the link (1) The number of links between File1 and File1hard becomes 2, (2) File1 and File1hard are the same in the Inode number, and (3) File1 and File1hard display the same file size.
The operation results of the LN command are visible: File1 and File1hard are two names of the same file, they have the same index node number and file attributes, and a hard link to the file file1 is to create a new pointer to the File1 file index node in the current directory.

The number of links is reduced by one, and the kernel removes the file contents from disk only if all pointers to the contents of the file, that is, the number of links is reduced to 0 o'clock. Current directory Logical Structure: (sorry, the graph is not shown).

You can also create hard links to files in different directories but on the same file system. Set File1, file2 in the directory/home/longcheng/dir1, the following command, in/home/longcheng to establish a hard link file2.

ln File2/home/longcheng/file2hard

The following program is to set up a hard link in the directory Dir2 for all files in the Dir1 directory

$mkdir dir2$ln/home/longcheng/dir1/*/home/longcheng/dir2

If the ln–f existingfile newfile is used, if the newfile already exists, the newfile becomes a hard-link file for NewFile, regardless of the original Exisitngfile file, only with the current user having write access to it.

Although hard links save space and are the traditional way for Linux systems to integrate file systems, there are shortcomings:
(1) No link can be established between files in different file systems
(2) Only Superuser can create a hard link for a directory. Although many trees say root user can create, but the author in the learning process found that even the root user cannot create, my system is redhat, kernel 2.4, 2.6 have tried, in other systems do not know whether it is possible.

Soft links (Symbolic links)

Soft links overcome the lack of hard links, without any file system restrictions, any user can create a symbolic link to the directory. It is now more widely used, it has greater flexibility, and can even link files across different machines and networks.

To create a soft link, simply add the option –s after Ln, as follows:

$ ls-il13058-rwx------1 Longcheng Longcheng 48 August 5 16:38 file113059-rwx-------2 Longcheng Longcheng 57 August 5 16:40 file213059-rwx------2 Longcheng Longcheng 57 August 5 16:40 File2hard
$LN –s file1 file1soft$ls-il13058-rwx------1 Longcheng Longcheng 48 August 5 16:38 file113059-rwx-------2 long Cheng Longcheng 57 August 5 16:40 file213059-rwx------2 Longcheng Longcheng 57 August 5 16:40 file2hard13061 lrwxrwxrwx 1 Longcheng Longcheng 5 August 5 16:58 file1soft->file1

From the results of the link above can be seen in soft links and hard links, the difference is not only conceptually, in the implementation is also different.

Difference:
Hard link Original file and link file common an inode number, stating that they are the same file, and the soft link original file and the linked file has different inode numbers, indicating that they are two different files.
Soft links on file properties are explicitly written out to be linked files, and hard links are not written out, because in essence the hard-link file and the original file are completely equal relationships.
The number of links is not the same, the number of links to soft links will not increase, the file size is not the same, hard-link file display size is the same as the original file.

In short, creating a soft link is the creation of a new file. When accessing a linked file, the system will find that he is a link file that reads the link file to find the file that is actually being accessed. Of course, soft links also have the disadvantage of hard links, because the link file contains the path information of the original file, so when the original file from one directory to another directory, and then access the linked file, the system will not be found, and the hard link does not have this flaw, you want to move how to move And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.