Linux linux study notes explain the connection of soft and hard links

 

0x00  linked files

Linux link two, one is called a hard link (Hard Link), the other is called a symbolic link (Symbolic Link). By default, ln command to generate a hard link.

[Soft link]

Another called symbolic link connection (Symbolic Link), also known as soft connection. There are soft-linked files like shortcuts for Windows. It is actually a special file. In the symbolic link, the file is actually a text file that contains the position information of another file.

Link files can even link file does not exist, and this problem is generally known as the "missing link" of (or say "phenomenon"), linked files can even link their own cycle. Similar to the programming language of recursion.

You may generate a command ln -s flexible connections, as follows:

[root@linux236 test]# ln -s source_file softlink_file

When the symbol file read or write operation, the system will automatically convert the operation to the operation of the source file, but delete the link file, the system simply delete the link file, without deleting the source file itself.

ps: to add soft link directory

 1, regardless of the source or destination file address file address must use an absolute path, or "too many layers of symbolic links" This error occurs

] [Hardwired
hard connection means to connect via the inode. 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). In Linux, multiple file names point to the same inode is there. In general this is a 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 real condition file deletion is associated with hard-wired all files are deleted.

info ln command tells you that hard link is another name for the file (A "hard link" is another name for an existing file) already exists, the number of which is a bit confusing. Command is hard-wired

ln -d existfile newfile 

There are two hard-linked files limit

1), allowed to create hard links to directories;

2) can only be created in the same file system links between files.

Hard linked files to read and write and delete operations when the same result, and soft links. But if we remove the hard-linked files source files, hard linked files still exist, but would like to retain some of the content.

At this time, the system will "forget" it used to be hard linked files. And put him as a regular file.

 

0x01  difference between the two

Hard-wired connection is performed by means inode. 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).

In Linux, multiple file names point to the same inode is there. In general this is a 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

Files 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

After the connection is deleted, the connection will block files and directories to be released. That is, the file will not actually be deleted.

Soft link file is somewhat similar to the Windows shortcut. It is actually a special file. In the symbolic link, the file is actually a text file that contains the position information of another file.

 

0x03 experiments to deepen understanding

[@ the Oracle Linux] $ Touch f1      # create a test file f1 
[@ the Oracle Linux] $ LN f1 F2      # a hard link files created f1 and F2 
[@ the Oracle Linux] $ LN -s f1 F3     # to create a symbolic link of f1 document F3 
[Oracle @ the Linux] -Li $ LS       # -i parameter displays the inode information file 
Total 0
 9,797,648-R & lt -rw - r-- 2 0 On Apr 21 is the oinstall Oracle 08:11 F1
 9,797,648 -rw-r-- On Apr Oracle the oinstall 0 2 r-- 21 is 08:11 F2
 9,797,649 lrwxrwxrwx Oracle. 1 2 On Apr 21 is the oinstall 08:11 F3 -> F1

As can be seen from the above results, the same hard-wired f1 f2 file, the original file is the inode, 9,797,648 are, however, different from the inode symbolic link file.

[oracle@Linux]$ echo "I am f1 file" >>f1
[oracle@Linux]$ cat f1
I am f1 file
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
I am f1 file
[oracle@Linux]$ rm -f f1
[oracle@Linux]$ cat f2
I am f1 file
[oracle@Linux]$ cat f3
cat: f3: No such file or directory

Through the above tests can be seen: when the original file is deleted f1, f2 hardwired affected, but the symbolic link file is invalid f1

 

0x04 summary

So you can do some of the relevant test, you can get all of the following conclusions:
1) delete symbolic links f3, for f1, no effect F2;.
2) delete hard-wired f2, to f1, f3 and has no effect;.
3). delete the original file f1, f2 is not hard-wired to the impact, led to symbolic links f3 failure;
4) deletes the original file f1, f2 hard-wired, the entire file will actually be deleted.

 

Guess you like

Origin www.cnblogs.com/JetpropelledSnake/p/11104002.html