• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            GLORY | 學習·記錄

            coding for life

            硬鏈接和軟連接

            文章轉自http://www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Unix/commands-links.html
            Hard links and Soft links

            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:

            • Hard links: a hard link is a pointer to the file's i-node. For example, suppose that we have a file a-file.txt that contains the string "The file a-file.txt":
              % 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

              Hard Links

              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!
              %
            • Soft links (symbolic links): a soft link, also called symbolic link, is a file that contains the name of another file. We can then access the contents of the other file through that name. That is, a symbolic link is like a pointer to the pointer to the file's contents. For instance, supposed that in the previous example, we had used the -s option of the ln to create a soft link:
              % ln -s a-file.txt b-file.txt
              On disk, the file system would look like the following picture:

              Soft Links

            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.txt

            Let 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 directory

            The 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.h

            posted on 2010-12-22 16:12 meglory 閱讀(365) 評論(0)  編輯 收藏 引用 所屬分類: Linux

            導航

            隨筆分類

            隨筆檔案

            最新評論

            亚洲第一永久AV网站久久精品男人的天堂AV | 久久精品夜夜夜夜夜久久| 久久精品国产乱子伦| 亚洲精品无码久久久久| 国产精品美女久久久久| 久久久久99精品成人片| 久久国产亚洲精品| 97久久综合精品久久久综合| 久久艹国产| 亚洲AV成人无码久久精品老人| 欧美777精品久久久久网| 一本一本久久a久久精品综合麻豆| AV无码久久久久不卡蜜桃| 日本精品久久久久中文字幕8| 无码乱码观看精品久久| 97久久超碰国产精品旧版| 色婷婷久久久SWAG精品| 日本一区精品久久久久影院| 久久人妻少妇嫩草AV蜜桃| 91麻精品国产91久久久久| 色狠狠久久AV五月综合| 久久久无码精品午夜| 久久电影网2021| 国产高潮国产高潮久久久| 色综合久久夜色精品国产| 大美女久久久久久j久久| 国产综合久久久久| 欧美va久久久噜噜噜久久| 免费久久人人爽人人爽av| 亚洲va久久久久| 亚洲七七久久精品中文国产| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 亚洲乱码精品久久久久..| 色播久久人人爽人人爽人人片AV| 亚洲精品国产成人99久久| 久久99国产精品久久99| 91久久婷婷国产综合精品青草| 狠狠色婷婷久久综合频道日韩| 中文字幕无码久久久| 久久久久久精品久久久久| 大香伊人久久精品一区二区 |