Hard-wired in the Linux (hard link) with a soft connection (symbolic link)

Reference article:
https://blog.csdn.net/gxzc936733992/article/details/49340429
https://www.cnblogs.com/jackhub/p/3779917.html
https://blog.csdn.net/bitboss/article / details / 53940236
on Linux (Ubuntu) ext4 file system, depending on the data, the memory can be divided into the following three types:

  • Attribute inode record file, a file occupies one inode, while this recordblock number of the data file resides
  • The actual contents of block record file, if the file is too large, it will take more than block;
  • super block of the file system records the entire information, including format information amount inode / block, the amount of the remaining amount, and the file system;

Linux file links the two, one is known as a hard link (Hard Link), the other is called a symbolic link (Symbolic Link). By default, ln order to produce a hard link, ln -s produce soft connection.

Hardwired Definition:
Hard connecting means to connect through the index section (the inode) points . Files in the Linux file system, stored on disk partition no matter what type it gave assign a number, called an inode number (Inode Index), display the command ls -li.In Linux, multiple file names point to the same inode is there, it typically is hard-wired connection.

The role of hard-wired to allow a file has multiple valid path name, so the user can create hard links to important documents, to prevent "accidental deletion" function. The reason described above, since there is more than one connection to be the directory inode. Only delete a connection does not affect the inode itself and other connections, only when the last connection is dropped, the connection data block and directory files will be released. In other words, the conditions are really delete files associated with hard-wired all files are deleted. And memory management "reference count" mechanism similar.

Limitation of Hard Links :

We use hard links also need to understand the limitations of hard links;

  1. Can not cross-file system
  2. You can not link to the directory (== soft links can link to the directory ==)

Analysis: The first point is well understood, we have the second point some explanation;
if you try to link a directory, the system will complain:
Here Insert Picture Description
because if you create a link to the directory, the directory is not the relationship itself of all documents relating to catalog, for example, you're in ld-hd directory hard links to create a new file, you must have a corresponding file in the directory ld, imagine if a link directory more, this is how many of the works, it is because of this complex reasons, Linux is not allowed to create hard links to directories.

] [Flexible connection
another connection called symbolic link (Symbolic Link), also known as soft connection. There are soft-linked files like shortcuts for Windows . It is actually a special text file, the contents of the location information is another file (create a soft link in the same directory, the contents of the file name of the document referred; created in different directories, content is the absolute path to the file within the meaning of ).

Here are some examples:


keney@keney-laptop ~> touch f1       #创建一个测试文件f1
keney@keney-laptop ~> ln f1 f2        #创建f1的一个硬连接文件f2
keney@keney-laptop ~> ln -s f1 f3    #创建f1的一个符号连接文件f3
keney@keney-laptop ~> ls -li            # -i参数显示文件的inode节点信息
#或者用stat命令来查看文件或文件系统的详细信息

1284 -rw-r--r-- 2 keney keney    0 2009-05-24 10:44 f1  #权限后面的2就是硬连接数
1284 -rw-r--r-- 2 keney keney    0 2009-05-24 10:44 f2
1340 lrwxrwxrwx 1 keney keney    2 2009-05-24 10:45 f3 -> f1 #修改日期前面的2是文件大小,
#说明软连接f3是个特殊的文本文件,里面保存者它所指向的文件f1的路径。
#inode index 1340后面的'l'说明是 链接,f1,f2是‘-’,普通文件。

As can be seen from the above results, the same hard-wired f1 f2 file, the original file is the inode, it is 1284, indicating that they are the same file. However, the inode different symbolic link file.

keney@keney-laptop ~> echo "I am f1 file" >> f1
keney@keney-laptop ~> cat f1
I am f1 file
keney@keney-laptop ~> cat f2
I am f1 file
keney@keney-laptop ~> cat f3
I am f1 file
keney@keney-laptop ~> rm -f f1
keney@keney-laptop ~> ls -li f2
1284 -rw-r--r-- 1 keney keney    0 2009-05-24 10:54 f2 #引用记数减1
keney@keney-laptop ~> cat f2
I am f1 file
keney@keney-laptop ~> cat f3
cat: f3: No such file or directory

When you delete the original file f1, f2 hardwired affected, but symbolic links f1 file is invalid

So visible:

1) Remove symbolic links f3, to f1, no f2 impact;
2) deleting hardwired f2, to f1, f3 no effect;.
3) delete the original file f1, no effect on the hardwired f2, resulting in a symbolic link F3. failure;
4) deletes the original file f1, hardwired f2, the entire file is actually deleted, then the reference count is decremented to zero.

Guess you like

Origin blog.csdn.net/u014157109/article/details/92396296