Usage and differences of soft links and hard links in file programming in Linux
In Linux, what is used to identify a file?
First, enter the command LS-I on the terminal to see what information will appear.
Think @ Ubuntu :~ $ LS-I
Displayed information
1048577 bin 523 initrd. IMG 524289 MNT 1 sys
1179649 boot 13 initrd. IMG. Old 655361 opt 793170
We can see that there is a number in front of each file. in Linux, this number is used to identify a file.
What are the names of these numbers? ---- index node number
Tiger-John summary;
You can run the LS-I command to view the index number of a file.
So what is an index Node? What is its function.
I. index nodes
The Linux File System uses an index node to record file information. The index node is a data structure, it contains information such as the file length, creation time, modification time, permission, ownership, and location on the disk. Each file or directory corresponds to an index node. The file system forms an array of all index nodes. The system assigns a number to each index node, that is, the index number of the node in the array, which is called the index node number. The file system uses this index node number to identify a file.
We mentioned above that a file is a link file. What does it do? What is its function?
1. Link
1> what is a link:
Create a new link to an existing file without copying the file content. No matter which file name is used
Can access the same file to improve file sharing.
2> in Linux, there are hard links and soft links (that is, what we call symbolic links.
3> A hard link is used to link a file to one or more file names, or to link the file names we use with the node numbers used by the file system, these file names can be in the same directory or different directories. A file has several file names, so we can say that the number of links to this file is several.
(Hard links give an alias to a file)
Disadvantages:
First, hard links cannot be created for directories.
Second, a link can be created only between files in the same file system.
People are always pursuing perfection. Therefore, to overcome the disadvantages of hard links, -----> symbol links appear.
4> symbolic links are actually a special file, which contains any path name of another file. This path name points to any file in any file system, and can even point to a non-existent file. The system automatically changes most operations on symbolic links (such as reading and writing) to operations on source files, but some operations (such as deleting) will be completed directly on the symbolic links.
Because symbolic links actually store the path name of a file, they can span directories and file systems. However, all these advantages are costly -----> A new index node must be created for soft links.
Bytes -----------------------------------------------------------------------------------------------------------
After talking about this, how can we use shell commands to establish a link to an existing file?
2. How to create a link for a file
1. Create a hard link for a file
First, enter the command ln tiger. c myhlink. C on the terminal.
An existing file tiger. C is implemented to create a hard link myhlink. C. What is the relationship between them?
(Hard links all point to the same file, so their index node numbers should be the same)
We can use the command to verify that ls-I can check whether their index numbers are the same.
Process instance:
Think @ Ubuntu :~ /Test $ ln tiger. c myhlink. c
Think @ Ubuntu :~ /Test $ LS-I
931356 myhlink. c 931356 tiger. c
The information shown above shows that their index numbers are the same.
2. Create a symbolic link for a file
First, enter the command ln-s tiger. c myslink. C on the terminal.
Then, enter the name "ls-I ".
To see if the index numbers are the same between them?
(Because soft links do not point to this file, they only store the address of the original file in this file, so their index numbers are different)
Process instance:
Think @ Ubuntu :~ /Test $ ln-s tiger. c myslink. c
Think @ Ubuntu :~ /Test $ LS-I
931346 myslink. c 931331 tiger. c
The above results show that the index numbers of the two are different.
Summary of tiger-JOHN:
1> use the shell command: ln filename1 filename2 to create a hard link filename2 for filename1.
2> use the shell command: ln-s filename1 filename2 to create a soft link filename2 for filename1.
3> run the shell command: ls-I to view the index number of the file.