<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    symbolic and hard link

    What is the difference between symbolic link and hard link? Explaining the difference between a symbolic link or a symlink and a hard link is easy and vital to knowing about how Linux/Unix environments work.


    Understanding the difference between a symbolic link and a hard link is also important for web server maintenance and understanding how modern websites work.

    What is symbolic link or symlink?

    Symbolic link, often called symlink or softlink, is very similar to what we know from Windows - a shortcut. They are kind of shortcuts in the Linux/Unix world. Well, symbolic link can exist in the Windows world too, but for the simplicity of our explanation, let's just work with the comparison that symlink is kind of a shortcut for now. We will get into more details later. Symbolic link contains information about the destination of the target file.

    What is hard link?

    Hard link (often also called hardlink) is a bit different object when compared to a symlink. Hard link is a directory reference or pointer to a file. Hardlink is a label stored in a directory structure that refers the operating system to the file data when it is accessed. The important part is that hard link is closely tied together with its originating file. If you make changes to a hard link, you automatically make changes to the underlying file that the hardlink is attached to.

    Hard link can only refer to data that exists on the same file system.

    Many of us are used to Windows where files live in folders. Files in Linux/Unix are not stored in directories. Files in Linux are assigned an inode number which Linux uses to locate files. Each file can have multiple hard links which are located in various directories. A file does not get deleted until there are no remaining hard links to it.

    Differences between symbolic link and hard link

    Let's summarize our findings. The list bellow summarizes some differences between symlink and hard link:

    Hardlink or hardlinks cannot be created for directories (folders). Hard link can only be created for a file.
    Symbolic links or symlinks can link to a directory (folder).
    Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still provides the content of the underlying file.
    If you remove the hard link or the symlink itself, the original file will stay intact.
    Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless (the same concept like Windows shortcut).
    In case you are interested getting to know even more details, this list is expanded more on the the Mklink page. The concept is simply that hard links are tied to their sources more rigidly.

    What is the difference between symlink and shortcut?

    We mentioned that symlinks are like shortcuts. They are like shortcuts but with some small differences.Symbolic links are automatically resolved by the file system. Any software programs, upon accessing a symbolic link, will see the target instead, whether the program is aware of symbolic links or not. On the other hand,shortcuts are treated like ordinary files by the files system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as Windows) treat shortcuts as references to other files. Shortcuts can point to files or directories that exist in another file system or on the network. The difference between symbolic link and shortcut is clearer from the example mentioned at the bottom of this article, respectively as shown in the big print screen on the Mklink page.

    How to create a hard link or hardlink?

    In Linux, you would use the ln command to create a hard link.

    $ ln fileA fileB

    where fileA is the original file and fileB is the name you want to give to the hardlink. Let's do some research now. You have the original file and one hard link that is attached to it. Now, you look at these two objects with the ls command:

    $ ls -il fileA fileB

    You can see in the output of this command that both files fileA and fileB have the same inode number (the first number on the line). In addition to having the same inode, both files have the same file permissions and the same size. Because that size is reported for the same inode, we can see that a hard link does not occupy any extra space on your space.

    If you now remove the original file and open the hard link, you will still be able to see the content of the original file.

    Note, hard link cannot be created to a folder. If you try creating a hard link to a folder, you will get "Access denied."

    How to create a symlink?

    Let's demonstrate this with an example. When talking about Linux, you would use the ln command with the -sparameter. You would do something like:

    $ ln -s fileA fileB

    where fileA is the original file and fileB is the name you want to give to the symbolic link. Now, let's take a look at these two objects with the ls command again:

    $ ls -il fileA fileB

    You can see that you get different result as compared to when we displayed the hard link. The first difference between symlink and the original file is the inode number. The inode is different for the original file and for the symbolic link. Next, you can also notice that there is the pipe symbol "l" before the permissions on the symlink line. Also, the symbolic link has different permissions than the original file (because it is just a symbolic link). The content of the symlink is just a string pointing to the original file. The size of the symlink is not the same as the size of the original file. The symbolic link is a separate entity and as such occupies some space on your hard drive. You can see at the end of the line where the symlink points to.

    You can access the content of the original file directly by calling the original file or by calling the symbolic link. You will see the same result.

    Now if you remove the original file, the symlink will still be there. If you try to access the content of the original file through the symbolic link after removing the original file, you will get a message saying there is no such file or directory.

    Can I make a symlink to a hard link?

    Yes. The hard link functions the same way like the original file; therefore, you can make symlinks to it. You would use in our example the following command:

    $ ln -s fileB fileC

    where fileB would be the name of the hard link, and fileC would be the name of your new symlink.

    QHow do I display file inode number using command line?

    AAn inode is a data structure on a Unix / Linux file system. An inode stores basic information about a regular file, directory, or other file system object. You can use following two commands to display an inode:
    [a] ls command : list directory contents

    [b] stat command : display file or file system status

    ls command

    Type ls command with -i ( print the index number of each file ) option:
    $ ls -i /etc/passwd
    Output:

    752010 /etc/passwd

    752010 is index number (inode) for /etc/passwd file.

    stat command

    $ stat /etc/passwd
    Output:

     File: `/etc/passwd' Size: 2026 
    Blocks: 8 IO Block: 4096 regular file
    Device: 811h/2065d Inode: 752010
    Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2007-08-20 23:19:20.000000000 +0530
    Modify: 2007-07-07 00:06:56.000000000 +0530
    Change: 2007-07-07 00:06:56.000000000 +0530

    You can use inode number to delete or search a file.

    Unix files consist of two parts: the data part and the filename part.

    The data part is associated with something called an 
    'inode'. The inode carries the map of where the data is, the file permissions, etc. for the data.


                                   
    .---------------> ! data ! ! data ! etc
                                  
    /                  +------+ !------+
            
    ! permbits, etc ! data addresses !
            
    +------------inode---------------+

    The filename part carries a name and an associated inode number
    .

                             
    .--------------> ! permbits, etc ! addresses !
                            
    /                 +---------inode-------------+
            
    ! filename ! inode # !
            +--------------------+
    More than one filename can reference the same inode number; these files are said to be 
    'hard linked' together.

            
    ! filename ! inode # !
            +--------------------+
                            
    \
                             
    >--------------> ! permbits, etc ! addresses !
                            
    /                 +---------inode-------------+
            
    ! othername ! inode # !
            +---------------------+
    On the other hand
    , there's a special file type whose data part carries a path to another file. Since it is a special file, the OS recognizes the data as a path, and redirects opens, reads, and writes so that, instead of accessing the data within the special file, they access the data in the file named by the data in the special file. This special file is called a 'soft link' or a 'symbolic link' (aka a 'symlink').

            ! filename ! inode # !
            +--------------------+
                            \
                             .-------> ! permbits, etc ! addresses !
                                       +---------inode-------------+
                                                          /
                                                         /
                                                        /
        .----------------------------------------------
    '
       ( 
        
    '-->  !"/path/to/some/other/file"! 
              +---------data-------------+
                      /                      }
        .~ ~ ~ ~ ~ ~ ~                       }-- (redirected at open() time)
       (                                     }
        
    '~~> ! filename ! inode # !
             +--------------------+
                             
    \
                              
    '------------> ! permbits, etc ! addresses !
                                             +---------inode-------------+
                                                                /
                                                               /
         .----------------------------------------------------
    '
        (
         
    '->  ! data !  ! data ! etc.
              +------+  +------+ 

    Now, the filename part of the file is stored in a special file of its own along with the filename parts of other files; this special file is called a directory. The directory, as a file, is just an array of filename parts of other files.

    When a directory is built, it is initially populated with the filename parts of two special files: the 
    '.' and '..' files. The filename part for the '.' file is populated with the inode# of the directory file in which the entry has been made; '.' is a hardlink to the file that implements the current directory.

    The filename part for the 
    '..' file is populated with the inode# of the directory file that contains the filename part of the current directory file. '..' is a hardlink to the file that implements the immediate parent of the current directory.

    The 
    'ln' command knows how to build hardlinks and softlinks; the 'mkdir' command knows how to build directories (the OS takes care of the above hardlinks).

    There are restrictions on what can be hardlinked (both links must reside on the same filesystem, the source file must exist, etc.) that are not applicable to softlinks (source and target can be on seperate file systems, source does not have to exist, etc.). OTOH, softlinks have other restrictions not shared by hardlinks (additional I/O necessary to complete file access, additional storage taken up by softlink file
    's data, etc.)

     



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-05-23 04:31 Chan Chen 閱讀(832) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 国产最新凸凹视频免费| 毛片高清视频在线看免费观看| 四只虎免费永久观看| 亚洲欧美日韩中文字幕在线一区| 毛片免费全部播放无码| 亚洲精品韩国美女在线| 永久黄色免费网站| 亚洲国产成人久久精品app| 日韩免费一区二区三区在线播放| 亚洲无线一二三四区| 91在线视频免费91| 亚洲欧洲AV无码专区| 国产免费拔擦拔擦8x| 一级毛片**免费看试看20分钟 | 亚洲高清免费视频| 一级特黄a大片免费| 亚洲日韩中文无码久久| 嫩草在线视频www免费观看| 亚洲日本在线免费观看| 在线精品免费视频无码的| 日本一区二区三区在线视频观看免费 | 成人免费777777被爆出| 亚洲AV中文无码字幕色三| 国产大片免费网站不卡美女| 亚洲最大福利视频| 亚洲精品久久久www| 最近免费中文字幕MV在线视频3 | 国产亚洲一卡2卡3卡4卡新区| 一本色道久久88亚洲综合 | 免费无码又爽又刺激毛片| 人人鲁免费播放视频人人香蕉| 亚洲开心婷婷中文字幕| 五月婷婷在线免费观看| 亚洲aⅴ天堂av天堂无码麻豆| 亚洲精品乱码久久久久久不卡| 久久免费视频网站| 亚洲国产综合AV在线观看| 色久悠悠婷婷综合在线亚洲| 1000部拍拍拍18勿入免费凤凰福利 | 亚洲精品制服丝袜四区| 成年女人免费v片|