1.
How to use: ln [option] source_file Dist_file (source_file is the file to be established for the linked file, Dist_file is the newly created link file)
-F is created, it is deleted with the file name.
-I ask before deleting.
LN-S ABC CDE establishes the soft connection of ABC
LN ABC CDE establishes the hard connection of ABC,
2. The difference between soft links and hard links (popular):
A hard link can be considered to be a file with two filenames; The soft link is the system creates a new link file, which points to the file it refers to. In addition, soft links can be made to files and folders. Hard links are only for files.
3. The difference between soft links and hard links (explained):
Linux soft and hard connections for a file, there is a unique index point corresponding to it, and for an index contact number, you can have multiple filenames 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, we usually use more soft connections.
eg
Ln-s Source Dist # establishing a soft connection
ln Source Dist # to establish a hard connection
Soft link is actually just a piece of text, which contains the name of the file it points to, the system sees the soft link automatically jumps to the corresponding file location for processing; instead, a hard join 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 appears to be equivalent. For this reason, a hard link cannot connect files on two different file systems.
(1) Soft connection can cross file system, hard connection is not possible. The practice is to use shared files to connect aa.txt text documents under Windows to Linux under the/root directory BB,CC. Ln-s Aa.txt/root/bb Connection succeeded. ln aa.txt/root/bb failed. (2) Questions about the I node. Hard connections No matter how many, all point to the same I node, will increase the number of node connections, as long as the junction number of connections is not 0, the file exists, regardless of whether you delete the source file or the connected file. As long as there is one exists, the file exists (in fact, it does not divide the source file connection files, because they point to the same I node). When you modify the source file or any one of the connection files, the other files will be changed synchronously. Soft links do not use the I node number directly as a file pointer, but instead use the file path masterpiece as a pointer. Therefore, deleting a connection file has no effect on the source file, but deleting the source file will cause the connection file to be unable to find the file to point to. The soft link has its own inode, and there is a small space on the disk to store the path name.
(3) A soft connection can be connected to a file name that does not exist.
(4) The soft connection can be connected to the directory.
Note: I node: it is a data structure used internally by UNIX to describe the characteristics of a file. We usually call the I node the file index node (information node). The I node contains important information about most of the file, including the address of the file data block on disk. Each I node has its own flag number, which we call the file sequence number. I node contains information 1. file type 2. File is the main relationship 3. Access to files 4. The time of the file is truncated.
finally delete the symbolic link, there is the deletion of the creation
RM-RF symbolic_name Note not rm-rf symbolic_name/
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). Symbolic connections are equivalent to shortcuts under Windows.
One, 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 newfile (sexistingfile file for link file, NewFile is the newly created link file)
ln [options] Existingfile-list Directory
-F is created, it is deleted with the file name.
-I ask before deleting.
LN-S ABC CDE establishes the soft connection of ABC
LN ABC CDE establishes the hard connection of ABC,
Usage:
The 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 link is created.
Here are some examples:
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
[Email protected] test]# LN file2 file2hand
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]#
Note Before you create a link, the number of links that File1 displays is 1, and the number of links file1 and File1hard are changed to 2;file1 and File1hard are the same in the inode number, as are the file sizes shown in File1 and File1hard. 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. You can delete any one of them, such as RM file2, and delete only one pointer at a time, minus one at a time, and the kernel will delete the file contents from disk when the number of links is reduced to 0 o'clock.
You can also create hard links to files in different directories but on the same file system. Set File1, file2 in the directory/home/root/dir1, the following command, in/home/root to establish a hard link file2.
ln File2/home/root/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/root/dir1/*/home/root/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.
Second, soft link (symbolic link)
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, just add the option –s after LN, for example
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]# ln-s file1 file1soft
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 File1soft-file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
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. The difference: the hard link original file and the 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 number, indicating that they are two different files; the soft link in the file attribute explicitly writes out the link file, and the hard link is not written. Because in essence the hard link file and the original file is completely equal, the number of links is not the same, the number of links to the soft link does not increase, the file size is not the same, the hard link file display size is the same as the original file, which is emphasized, because it is the same, and here Soft link display size and the original file is different File1 size is 48B, and File1soft is 5B, 5 of which is actually the size of "file1".
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.
To establish a soft link between different systems, to establish a link to the directory, here is not an example, the reader can try themselves, I am also in the practice of learning.
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 can not find the ~ ~, and hard links do not have this flaw, you want to move how to move (hehe) And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.
Add: Symlink can be used to view linked files, you can use the man symlink to learn.
Third, delete the link
There is a delete when created
RM-RF Symbolic_nameAttention, not RM-RF symbolic_name/.
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 0 07-14 14:17 file1
1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 File1soft-file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]# RM-RF File1soft
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 0 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]#
————————————
The difference between a Linux soft connection and a hard link:
————————————
4 points Different:
(1) Soft connection can cross file system, hard connection is not possible.
The practice is to use shared files to connect aa.txt text documents under Windows to Linux under the/root directory BB,CC. Ln-s Aa.txt
/root/bb connection succeeded. ln aa.txt/root/bb failed.
(2) Questions about the I node. Hard connections No matter how many, are pointing to the same I node, will increase the number of node connections, as long as the node is not 0 connections, the file has been there, whether you delete the
The source file or the connected file. As long as there is one exists, the file exists (in fact, it does not divide the source file connection files, because they point to the same I node). When you modify the source file or the connection file
At any one time, the other files will be modified synchronously. Soft links do not use the I node number directly as a file pointer, but instead use the file path masterpiece as a pointer. So deleting a connection file has no effect on the source file, but
is to delete the source file, the connection file will not find the file to point to. The soft link has its own inode, and there is a small space on the disk to store the path name.
(3) A soft connection can be connected to a file name that does not exist.
(4) The soft connection can be connected to the directory.
Note: I node: it is a data structure inside UNIX that describes the characteristics of a file. We usually call the I node the file index node (information node). I node contains important information about most of the files, including the file data block
The address on the disk. Each I node has its own sign number, which we call the file sequence number. I node contains information 1. file type 2. File is the main relationship 3. Access to files 4. The time of the file is truncated.
Use of Linux links to create and delete symbolic connections (soft, hard links)