在Linux系統中,內核爲每個新建立的文件分配一個Inode(索引結點),每一個文件都有一個唯一的inode號。文件屬性保存在索引結點裏,在訪問文件時,索引結點被複制到內存在,從而實現文件的快速訪問。 html
硬連接說白了是一個指針,指向文件索引節點,系統並不爲它從新分配inode。能夠用:ln命令來創建硬連接。語法: node
ln [options] existingfile newfile ln [options] existingfile-list directory用法:
第一種:爲」existingfile」建立硬連接,文件名爲」newfile」。 linux
第二種:在」directory」目錄中,爲 」existingfile-list」中包含的全部文件建立一個同名的硬連接。經常使用可選[options] –f 不管」newfile」存在與否,都建立連接。-n 若是」newfile」已存在,就不建立連接。 ide
因爲硬連接是有着相同 inode 號僅文件名不一樣的文件,所以硬連接存在如下幾點特性: this
軟連接與硬連接不一樣,若文件用戶數據塊中存放的內容是另外一文件的路徑名的指向,則該文件就是軟鏈接。軟連接就是一個普通文件,只是數據塊內容有點特殊。軟連接有着本身的 inode 號以及用戶數據塊(見 圖 2.)。所以軟連接的建立與使用沒有相似硬連接的諸多限制: spa
(1)因爲不一樣的分區會有相同的inode及硬鏈接的特殊性和文件系統的可卸載性,致使硬鏈接不允許跨文件系統(分區)!而 soft link 具備完整的 pathname,因此他能夠跨越不一樣文件系統。 unix
(2) 硬鏈接不會建產新的inode,硬鏈接無論有多少個,都指向的是同一個inode節點,只是新建一個hard link會把結點鏈接數增長,只要結點的鏈接數不是0,文件就一直存在,無論你刪除 的是源文件仍是鏈接的文件。只要有一個存在,文件就存在(其實也不分什麼源文件鏈接文件的,由於他們指向都是同一個 inode節點)。當你修改源文件或者鏈接文件任何一個的時候,其餘的文件都會作同步的修改 。 指針
軟連接不直接使用inode節點號做爲文件指針,而是使用文件路徑名做爲指針。因此刪除鏈接文件對源文件無影響,可是刪除源文件,鏈接文件就會找不到要指向的文件。軟連接有本身的inode,並在磁盤上有一小片空間存放路徑名. code
(3)軟鏈接能夠對一個不存在的文件名進行鏈接。 orm
(4)軟鏈接能夠對目錄進行鏈接。
ln -s source dist # 創建軟鏈接
ln source dist # 創建硬鏈接
Links
As was mentioned in the section on file system structure, every file has a data structure (record) known as an i-node that stores information about the file, and the filename is simply used as a reference to that data structure. A link is simply a way to refer to the contents of a file. There are two types of links:
% cat a-file.txt The file a-file.txt %
Now we use the ln command to create a link to a-file.txt called b-file.txt:
% ls ./ ../ a-file.txt % ln a-file.txt b-file.txt % ls ./ ../ a-file.txt b-file.txt
The two names a-file.txt and b-file.txt now refer to the same data:
% cat b-file.txt The file a-file.txt %
If we modify the contents of file b-file.txt, then we also modify the contents of file a-file.txt:
% vi b-file.txt ... % cat b-file.txt The file a-file.txt has been modified. % cat a-file.txt The file a-file.txt has been modified. %
and vice versa:
% vi a-file.txt ... % cat a-file.txt The file a-file.txt has been modified again! % cat b-file.txt The file a-file.txt has been modified again! %
% ln -s a-file.txt b-file.txtOn disk, the file system would look like the following picture:
But what are the differences between the two types of links, in practice? Let us look at an example that highlights these differences. The directory currently looks like this (let us assume that a-file.txt b-file.txt are both hard links to the same file):
% ls ./ ../ a-file.txt b-file.txtLet us first add another symbolic link using the -s option:
% ln -s a-file.txt Symbolicb-file.txt % ls -F ./ ../ a-file.txt b-file.txt Symbolicb-file.txt@A symbolic link, that ls -F displays with a @ symbol, has been added to the directory. Let us examine the contents of the file:
% cat Symbolicb-file.txt The file a-file.txt has been modified again!If we change the file Symbolicb-file.txt, then the file a-file.txt is also modified.
% vi Symbolicb-file.txt ... % cat Symbolicb-file.txt The file a-file.txt has been modified a third time! % cat a-file.txt The file a-file.txt has been modified a third time! % cat b-file.txt The file a-file.txt has been modified a third time! %If we remove the file a-file.txt, we can no longer access the data through the symbolic link Symbolicb-file.txt:
% ls -F ./ ../ a-file.txt b-file.txt Symbolicb-file.txt@ % rm a-file.txt rm: remove `a-file.txt'? y % ls -F ./ ../ b-file.txt Symbolicb-file.txt@ % cat Symbolicb-file.txt cat: Symbolicb-file.txt: No such file or directoryThe link Symbolicb-file.txt contains the name a-file.txt, and there no longer is a file with that name. On the other hand, b-file.txt has its own pointer to the contents of the file we called a-file.txt, and hence we can still use it to access the data.
% cat b-file.txt The file a-file.txt has been modified a third time!Although it may seem like symbolic links are not particularly useful, hard links have their drawbacks. The most significant drawback is that hard links cannot be created to link a file from one file system to another file on another file system. A Unix file structure hierarchy can consist of several different file systems (possibly on several physical disks). Each file system maintains its own information regarding the internal structure of the system and the individual files on the system. Hard links only know this system-specific information, which make hard links unable to span file systems. Soft links, on the other hand, know the name of the file, which is more general, and are able to span file systems.
For a concrete analogy, suppose that our friend Joel User is a student at both UBC and SFU. Both universities assign him a student number. If he tries to use his UBC student number at SFU, he will not meet with any success. He will also fail if he tries to use his SFU student number at UBC. But if he uses his legal name, Joel User, he will probably be successful. The student numbers are system-specific (like hard links), while his legal name spans both of the systems (like soft links).
Here is an example that demonstrates a situation where a hard link cannot be used and a symbolic link is needed. Suppose that we try to create a hard link from the current working directory to the C header stdio.h.
% ln /usr/include/stdio.h stdio.h ln: creating hard link `stdio.h' to `/usr/include/stdio.h': Invalid cross-device link %The ln command fails because stdio.h is stored on a different file system. If we want to create a link to it, we will have to use a symbolic link:
% ln -s /usr/include/stdio.h stdio.h % ls -l lrwxrwxrwx 1 a1a1 guest 20 Apr 20 11:58 stdio.h -> /usr/include/stdio.h % ls ./ ../ stdio.h@ %Now we can view the file stdio.h just as if it was located in the working directory. For example:
% cat stdio.h /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */ /* The copyright notice above does not evidence any */ /* actual or intended publication of such source code. */ /* * User-visible pieces of the ANSI C standard I/O package. */ #ifndef _STDIO_H #define _STDIO_H ... %The entire output of the cat command was not included to save space.
Note that the long listing (ls -l) of a soft link does not accurately reflect its associated permissions. To view the permissions of the file or directory that the symbolic link references, the -L options of the ls command can be used. For example:
% ln -s /usr/include/stdio.h stdio.h % ls -l stdio.h lrwxrwxrwx 1 a1a1 undergrad 20 May 10 15:13 stdio.h -> /usr/include/stdio.h % ls -l /usr/include/stdio.h -rw-r--r-- 1 root bin 11066 Jan 5 2000 /usr/include/stdio.h % ls -lL stdio.h -rw-r--r-- 1 root bin 11066 Jan 5 2000 stdio.hKnowledge of links becomes doubly important when working as part of a software development team, and even more so when using a source code management tool such as RCS. The man page for the ln command contains more information on Unix links and the ln.