linux system hard links and soft links

First, we need to understand the basic concepts of soft and hard links connected under linux.
Hard link: the new file already exists an alias file when the original file is deleted , the new file can still be used.
Soft links: Also known symbol link, the new file as a "path" to indicate another file, and is very similar to Windows shortcuts, the new soft links can point to a file that does not exist.
the following details about the difference between hard and soft link connection.
1. hard links and the original file is no different, but also shared number (file uniquely identifies on the file system) a inode; and soft link is not shared inode, it can be said is a special inode, it and the original inode differentiated .
2. If the original file is deleted , and the connection will not have access to soft, and hard connection is possible.
3. Due to the nature of a symbolic link, which can lead to cross partition , but hard links do not have this characteristic.
Hard link (hard link) ln
command format is: ln [option] Source file destination

Options: If you do not add "-s", hard-linked files is established. Plus "-s", a soft link is established.

-f: Force, if the target file already exists, then delete the target file after the establishment of linked files.

Demonstration follows

命令为:ln sh1.cpp hard
命令:cat hard
#include<iostream>

using namespace std;

class Demo
{
        public:
                Demo()
                {
                        cout << "Now the constructor is running.\n";
                }
};

int main()
{
        cout << "This is displayed before the object is created .\n";

        Demo demobj;

        cout << "This is displayed before the object is treated .\n";
        return 0;
}
命令:cat sh1.cpp #include
<iostream> using namespace std; class Demo { public: Demo() { cout << "Now the constructor is running.\n"; } }; int main() { cout << "This is displayed before the object is created .\n"; Demo demobj; cout << "This is displayed before the object is treated .\n"; return 0; }

 

The characteristics of hard links

1. Whether to modify the source file or modify hard-linked files, other data in the file will change.

2. Regardless of the source file is deleted or delete hard links to files, as long as there exists a file, the file will be accessed.

3. Hard links do not create a new inode information, it will not change the total number of the inode.

4. Limitation of Hard Links more, that can not cross file system. You can not link directories, and files and hard links between source files in addition to the inode number is the same as outside, no other obvious features.


Soft link (symbolic link) ln-s
soft link can be established directly, it can also be established through the directory.

Presentation as follows:

 

Softlinks direct 
command: LN -s sh2.cpp Soft 
(must be absolute path) through the soft links directory 
command: ln -s /home/qqtsj/cpp/sh3.cpp / home / qqtsj / cpp / swor 

following soft link created, view the 
command: LS the -l 
-rw-rw-r-- 1 qqtsj qqtsj 18:52 size.cpp 289 11 Yue 7 
lrwxrwxrwx 1 qqtsj qqtsj 7 1 Yue 19:27 Soft 5 -> sh2.cpp 
lrwxrwxrwx 1 qqtsj qqtsj 23. 1 dated SWOR 19:30. 5 -> /home/qqtsj/cpp/sh3.cpp 
-rwxrwxr X-29336. 11. 1 qqtsj qqtsj 17:07 Tate dated 12 is 
-rwxrwxr qqtsj qqtsj 13280-X. 1 at 20:38 on November 10 tate1 
-rw-r-- RW-677. 11. 1 qqtsj qqtsj 20:46 tate1.cpp dated 10 
-rw-r-- RW-0. 11. 1 qqtsj qqtsj dated 10 20:37 tate2.cpp

 

 

 

Soft link characteristics

1. Whether to change the source file, or change the soft link file, another file of the data will change.

2. Remove the soft links, source files will not be affected, and delete the source file, the file will not find the soft link actual data to display file does not exist.

3. soft link will create your own block and inode information, but does not store the actual file data in the block, and is stored in the file name and inode number of the source file.

4. Soft links can link directory, you can boast partition.

Hard and Soft Links biggest difference is that in principle: hard links do not build their own index inode and block (data block), but directly to the inode information and block the source file, so the inode number of source files and hard links is the same: while the soft link will be established its own inode index and block, so the inode number of soft links and source files are inconsistent, but also in the soft link block, the writing is not real data, but only the source file file name and inode number.

Guess you like

Origin www.cnblogs.com/tanshengjiang/p/12153379.html