The difference and summary of soft links and hard links in Linux

Source: Internet
Author: User

ln command
This command creates a link between the files. This kind of operation is actually to give a file that already exists in the system to specify another name that can be used to access it. For this new file name, we can specify different access rights to control the sharing and security of the information. If the link is to a directory, the user can use the link to go directly to the linked directory without having to hit a lot of path names. Moreover, even if we delete this link, it will not destroy the original directory.
Syntax: ln [options] target [link name]
ln [options] Destination directory
There are two types of links, one called hard links, and the other called Symbolic links (symbolic link). When a hard link is established, the linked file and the linked file must be in the same file system, and a hard link to the directory cannot be established. For symbolic links, there is no such problem. By default, LN produces a hard link.
In the case of a hard link, the "target" in the parameter is linked to [link name]. If the [link name] is a directory name, the system will create one or more linked files with the same name as "target" in this directory, the link file and the contents of the linked file are identical. If the [link name] is a file, the user will be told that the file already exists and is not linked. If more than one target parameter is specified, the last parameter must be a directory.
If you add the-s option to the LN command, the symbolic link is established. If the [link name] already exists but is not a directory, the link will not be made. A link name can be either a file name (which can contain a path) or a directory, and it is allowed to be in a file system other than "target". If the [link name] is a directory that already exists, the system will establish one or more files with the same name as "target" in this directory, which is actually a symbolic link file pointing to the original "target".
================================================================
For a file, there is a unique index point (inode) corresponding to it, and for an index contact number, there can be more than one file name corresponding to it. Therefore, the same file on the disk can access the file through a different path. Note that under Linux everything is file, folder, new HDD ... Can look at the file to deal with AH.
A soft connection (symbolic link) is also called a symbolic connection, connected with a soft connection and a hard link. Symbolic connections are equivalent to shortcuts under Windows.
It is not possible to make hard connections to folders/directories, we usually use more soft connections.

eg

Ln-s Source Dist # establishing a soft connection

ln Source Dist # to establish a hard connection

What difference do they have? The last time also good to see, as if the structure of the file system has a certain relationship, not much detailed study next time to see clearly plus.
----------------------
A soft link is actually just a piece of text, which contains the name of the file it points to, and the system sees the soft link automatically jumps to the corresponding file location for processing; instead, a hard link opens a new directory entry for the file, and the hard links and the original names of the files are equal, and in Linux it seems they are equivalent. For this reason, a hard link cannot connect files on two different file systems.
Soft connections are similar to shortcuts under Windows
As for the hard connection, for example, you put the dir1/file1 hard to Dir2/file2, is to create a dir1/file1 image file under Dir2 file2, it is occupied with the file1 of the same space, and change one of the two, The same changes will occur in the other.
Soft joins and hard joins can be understood as:
A hard connection is like a file with multiple filenames,
A soft connection is the creation of a new file (the contents of this file, which is actually the information to link to the original file path), the file points to the location of the other file,
Hard connections must be in the same file system, and soft connections can cross file systems
Hard connection: Both the source file name and the link file name point to the same physical address, the directory can not have a hard connection, the file has only one copy on the disk, can save hard disk space, because the deletion of the file to the same index node belonging to a unique connection can be successful, so you can prevent unnecessary accidental deletion soft connection (symbolic connection) -S command to create a symbolic connection of a file, a symbolic connection is a special file of Linux, as a file, its data is the path name of the file it connects to, similar to the hardware way, ****** can delete the original file and the connection file still exists. ********
This article attempts to clearly and thoroughly explain the differences between soft links and hard-link files
A linked file
Links are available in two ways, soft links and hard links.
1 Soft link files
A soft link is also called a symbolic link, which contains the path name of another file. can be any file or directory that can link files from different file systems.
Linked files can even link non-existent files, which results in what is commonly referred to as a "broken chain" (or "phenomenon"), linked files can even be linked to the loop itself. Similar to recursion in a programming language.
Use the Ln-s command to generate a soft connection, as follows:
[Email protected] test]# ln-s source_file softlink_file
When you read or write to the symbol file, the system automatically converts the operation to the source file, but when you delete the linked file, the system simply deletes the linked file without deleting the source file itself.
2 Hard-Link files
The Info ln command tells you that hard links are another name for a file that already exists (A ' tough link ' is another name for an existing file), which is somewhat confusing. The hard-connect command is
ln-d Existfile NewFile
There are two restrictions on hard-link files
1), do not allow to create hard links to the directory;
2), you can create a hard link only between files in the same file system.
When you read and write to a hard-link file, the result is the same as a soft link. But if we delete the source file of the hard-link file, the hard-link file still exists, and the content of the wish is preserved.
At this point, the system "forgot" it was once a hard link file. And treat him like a regular file.
The difference between the two
A hard join refers to a connection made through an index node. In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode #).
In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have more than one valid pathname so that the user can establish a hard connection to an important
Files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, only if the last
The connection to the file's data block and directory will be released when the connection is deleted. In other words, the file is actually deleted.
A soft link file is a bit like a shortcut to Windows. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains location information for another file.
Three people experience
The soft link is another file, the function can be understood as a pointer, ****** action on this file operation in addition to the deletion are directly to the actual point of the file, because it is a real file, so occupy disk space
A hard link can be considered not a file, it is just an alias of the actual file, its role is to prevent the real file is mistakenly manipulated, to a file after the establishment of a hard link, they alias each other, delete any of them,
This will only remove the alias with the RM command, and the actual file will not be deleted. The original file is deleted only if the number of links is 0 o'clock

The difference and summary of soft links and hard links in Linux

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.