Differences between hard links and soft links (symbolic connections), links and soft links

Source: Internet
Author: User

Differences between hard links and soft links (symbolic connections), links and soft links
In linux, ln commands are provided to link files. File links are mainly divided into hard links and soft links.

Hard link: Because files in linux are identified by Inode, hard links can be considered as a pointer to the file index node, the system does not re-allocate inode for it. Each time a hard link is added, the number of links to the file is increased by 1.

You can use the ln command to create a hard link. Syntax:

[Php]View plaincopy
  1. Ln [options] existingfile newfile
  2. Ln [options] existingfile-list directory

Usage: the first method is to create a hard link for "existingfile" and the file name is "newfile ". Type 2: Create a hard link with the same name for all files contained in "existingfile-list" in the "directory" directory. Commonly used [options]-f creates a link regardless of whether "newfile" exists or not. -N if "newfile" already exists, no link is created.

Example:

 

[Php]View plaincopy
  1. $ Ls-il
  2. 13058-rwx---1 longcheng 48 Aug 16:38 file1
  3. 13059-rwx---1 longcheng 57 Aug 17 16:40 file2
  4. $ Ln file2 file2hard
  5. $ Ls-il
  6. 13058-rwx---1 longcheng 48 Aug 16:38 file1
  7. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2
  8. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2hard

Note: before creating a link, the number of links displayed in file1 is 1. After the link is created, (1) the number of links in file1 and file1hard is changed to 2. (2) file1 and file1hard are the same in inode (3) the file size displayed by file1 and file1hard is the same. The result of ln command is as follows: file1 and file1hard are two names of the same file. They have the same index node number and file attributes and create a hard link to file1, creates a new pointer for the file index node of file1 in the current directory. For example, you can delete any of them, such as rm file2. Only one pointer is deleted at a time, and the number of links is reduced by one. Only pointers pointing to the file content are allowed, that is, when the number of links is reduced to 0, the kernel will delete the file content from the disk.

Disadvantages:

Although hard links save space, they are also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) You cannot establish links between files in different file systems (2) only Super Users can create hard links for directories.

 

 

Soft link (symbolic link ):

Soft links overcome hard links without any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks.

If the-s option is added to the ln command, a soft link is established. If the [Link name] already exists but is not a directory, no link will be made. [Link name] can be any file name (including path), a directory, and allow it to be different from the target file system. If the [Link name] is an existing directory, the system creates one or more files with the same name as the "target" in the directory, the new file actually points to the symbolic link file of the original "target.

Example:

[Php]View plaincopy
  1. $ Ls-il
  2. 13058-rwx---1 longcheng 48 Aug 16:38 file1
  3. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2
  4. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2hard
  5. $ Ln-s file1 file1soft
  6. $ Ls-il
  7. 13058-rwx---1 longcheng 48 Aug 16:38 file1
  8. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2
  9. 13059-rwx--2 longcheng 57 Aug 17 16:40 file2hard
  10. 13061 lrwxrwxrwx 1 longcheng 5 Aug 17 16:58 file1soft-> file1

 

Soft links and hard links differ not only in terms of concept, but also in terms of implementation. Difference: A Public inode number is used for hard-link files and linked files, indicating that they are the same file, while soft-link files and linked files have different inode numbers, it indicates that they are two different files. On the file attributes, the soft link clearly writes the link file, but the hard link is not written, in essence, hard-link files are completely equal to the original files. The number of links is different, and the number of soft-link links does not increase. The file size is different, the size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, and the size of the soft link is different from that of the original file, the size of file1 is 48B, while that of file1soft is 5B, where 5 is actually the size of "file1.

In short, creating a soft link is to create a new file. When you access a linked file, the system will find that it is a linked file. It reads the linked file and finds the file to be accessed.

 

Disadvantages:

Because the linked file contains the path information of the original file, when the original file is moved from one directory to another, and then the linked file is accessed, the system cannot find it, however, hard links do not have this defect. You need to move them as much as you want. In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path.


What is a link? What is the difference between a symbolic link and a hard link?

One of the most important features of a Linux file system is its file link. The link is a reference to the file, so that you can see the file in the file system. However, in Linux, links can be treated as original files. Links can be executed, edited, and accessed like normal files. For other applications in the system, the link is the original file corresponding to it. When you edit a file through a link, you actually edit the original file. The link is not a copy. There are two types of links: hard link and symbolic link.
Hard links can only reference files in the same file system. It references the physical indexes (also known as inode) of files in the file system ). When you move or delete the original file, the hard link will not be broken because it references the physical data of the file rather than the position of the file in the file structure. A hard-linked file does not require the user to have the permission to access the original file or display the location of the original file. This helps the file security. If the deleted file has a hard link, the file will still be retained until all references to it are deleted.
A symbolic link is a pointer pointing to the position of a file in the file system. Symbolic Links can point to files in a remote file system across file systems. The symbolic link only specifies the location of the original file. You must have access to the location of the original file to use the link. If the original file is deleted, all symbolic links pointing to it will be damaged. They point to a location that does not exist in the file system. Both links can be created using the ln command. By default, ln creates hard links. You can use the-s switch to create a symbolic link.
What is the difference between a symbolic link and a hard link?
Simply put, hard connection records the inode of the target, and the symbolic connection records the path of the target.
Soft connections are like shortcuts, while hard connections are like backups! The symbolic connection can be used as a link across partitions, while the hard connection can only be used as a link in this partition due to inode. Therefore, the usage frequency of symbolic connections is much higher.

What are the concepts of hard connection and symbolic connection in linux?

What is the difference between a hard link and a symbolic link? The Symbolic Link must contain the-s option, instead of the hard link, and the path followed, the path followed by the hard link must be written to the file that creates the symbolic link. The path followed by the symbolic link is a folder that contains the hard link. For example:
Ln-s/etc/httpd/conf/httpd. conf/etc/creates a symbolic link file for the file/etc/httpd/conf/httpd. conf and saves it to the/etc directory.
Ln/usr/sbin/system-config-network/sbin/mynetconfig create a hard link file named mynetconfig for the NIC configuration script file/usr/sbin/system-config-network

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.