In Linux ln command--hard links and soft links __linux

Source: Internet
Author: User

It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.


One, ln command parameters

LN is another very important command in Linux, its function is to create a link for a file in another location, the most common parameter of this command is-s, which is the Ln–s source file destination file.
When we need to use the same file in a different directory, we don't need to put a file that must be the same in every directory we need, we just put the file in a certain directory, and then use the LN command link in the other directory to make it available, without duplicating the disk space. For example: ln–s/bin/less/usr/local/bin/less
-S is the meaning of the Code name (symbolic).
Here are two points to note: first, the LN command keeps the synchronization of each linked file, which means that no matter which one you change, the other files will change the same; second, the links in Ln are soft links and hard links, and soft links are ln–s * * * *, It will only generate a mirror image of the file in your chosen location, does not occupy disk space, hard link LN * * * * * *, no parameter-s, it will be in the location you choose to generate a file with the same size as the source file, whether soft links or hard links, the files are kept synchronized changes.
If you use LS to look at a directory, found that there is a file behind a @ symbol, that is a file in the ln command generated, with the ls–l command to view, you can see the path of link displayed.
Instructions detailed instructions
Directive Name: LN
Use Rights: All users
How to use: ln [options] Source dist, where option is in the form of:
[-BDFINSVF] [-S Backup-suffix] [-v {numbered,existing,simple}]
[--help] [--version] [--]
Description: Linux/unix file system, there is a so-called link, we can treat it as a file alias, and the link can be divided into two kinds: Hard link (hardlink) and soft link (symboliclink), hard link means that a file can have more than one name, The soft link is to create a special file that points to the location of another file. Hard links exist in the same file system, while soft links can span different file systems.
The LN source Dist produces a link (dist) to source, as with a hard link or a soft link is determined by the parameter.
Either a hard link or a soft link will not copy the original file, it will only occupy a very small amount of disk space.
-F: First delete files with dist file name when linking
-D: Allows system managers to hard link their own directories
-I: Ask before deleting files with the same file name as Dist
-N: Dist is considered a general file when soft links are made
-S: Soft link (symbolic link)
-V: Displays the file name before the link
-B: Backup files that will be overwritten or deleted when the link is made
-S SUFFIX: Add the backup file to the end of the SUFFIX
-V Method: Specify how the backup should be
--HELP: Show Auxiliary Instructions
--version: Display version
Example:
The file yy generates a symbolic link:zz
Ln-s yy ZZ
The file yy generates a hard link:zz
ln yy XX


two, hard link and soft connection difference: to illustrate this issue, first explain the nature of the files and directories under Liunx.
In fact, on the Liunx, the directory is also a kind of file, it is stored a table of files. For example, there is a folder called a program, which has two files 1 and 2. In that table of contents. It's the content of this
Name node
1 338
2 228
So what is a node? C language We have learned, we simply understand the node number as an array of subscript, the memory as a large array, each file can be seen as an element of an array, and know the node number, you can find the actual content of the file.

With the above understanding, you can further explain the hard links:
The hard link writing format is: ln target filename Link name
So what is the process of it?
For example we enter: LN 3 2
Then, add an item under the same table of contents
Name node
1 338
2 228
3 228
At this point, file 3 points to the same memory block as 2, which means that the contents of 2 are exactly the same.
And the soft connection is what's going on.
It must be noted that soft and hard links are also special files, in the Liunx are all in the file, soft connection can be regarded as a text file, its content is to save the destination file name path address.
Soft connection format is ln-s target filename link name
For example, enter Ln-s 2 4
Its execution is this, first copy the path name of 2 files to 4, perform 4 o'clock, first read the pathname from 2, find 2 of this file, and then execute 2. Therefore, the operation of 4 files is the operation of 2 files.

The above illustrates the specific principle. If you want to understand the popular point. You can take a hard link as a copy of a source file that shows the same size as the source file but does not actually occupy any space. A soft connection can be a great understanding of Windows shortcuts.

The difference between a hard link and a soft link

A linked file

The first thing to figure out is that in a Linux system, the kernel assigns an Inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are stored in the index node, and when the file is accessed, the index nodes are copied to the inside to enable fast 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. Linux includes two kinds of links: Hard link (Hard link) and soft link (Soft link), soft link is also known as symbolic link (symbolic link).

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, you can link files of different file systems.
Linked files can even link nonexistent files, which produces what is commonly called a "broken chain" problem (or "phenomenon"), linked files can even be linked to the loop itself. Similar to recursion in a programming language.
A soft connection can be generated with the Ln-s command, as follows:
[root@linux236 test]# ln-s source_file softlink_file
When you read or write a symbol file, the system automatically converts the action 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 a hard link is another name for a pre-existing file (A "Hard link" is another name to an existing file), which is somewhat confusing. The Hard connection command is
ln-d Existfile NewFile
Hard link file has two restrictions
1), not allowed to create a hard link to the directory;
2. Links can only be created between files in the same file system.
The results are the same as soft links when read and write to hard linked files. However, if we delete the source file of the hard link file, the hard link file still exists, and the content is reserved.
At this point, the system "forgot" it used to be a hard link file. and regard him as an ordinary document.
The difference between the two

A hard connection is a connection made through an index node. In a Linux file system, a file stored in a disk partition is assigned a number, called an index node number (Inode index), regardless of its type.
In Linux, multiple file names point to the same index node. Generally this connection is a hard connection. The effect of a hard connection is to allow a file to have multiple valid pathname so that the user can establish a hard connection to the important
Files to prevent "accidental deletion" of the function. The reason for this is as described above because there is more than one connection to the index node of the directory. Deleting only one connection does not affect the index node itself and other connections, only when the last
When a connection is deleted, the file's data block and directory connections are released. That is to say, the file is actually deleted.

Soft link files are somewhat similar to Windows shortcuts. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains the location information for another file.

Three   personal experience
    Soft link is another file, the role can be understood as a pointer, action on this file in addition to the deletion of the operation directly to the actual point file, because it is a real file so occupy disk space
    hard links can be considered not a file, it is just an alias of the actual file, its role is to prevent the real files from being mistakenly manipulated, to create a hard link to a file, they alias each other, delete any of them,
     only deletes the alias, and the actual file is not deleted. Because the alias does not have any other information, it does not occupy the disk space of the original file size.

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.