What is there behind a symbolic link?

前端 未结 2 1018
臣服心动
臣服心动 2020-12-15 05:48

How are symbolic links managed internally by UNIX/Linux systems. It is known that a symbolic link may exist even without an actual target file (Dangling link). So what is th

2条回答
  •  失恋的感觉
    2020-12-15 06:16

    You can also easily explore this on your own:

    $ touch a
    $ ln -s a b
    $ ln a c
    $ ls -li
    total 0
    95905 -rw-r--r-- 1 regnarg regnarg 0 Jun 19 19:01 a
    96990 lrwxrwxrwx 1 regnarg regnarg 1 Jun 19 19:01 b -> a
    95905 -rw-r--r-- 2 regnarg regnarg 0 Jun 19 19:01 c
    

    The -i option to ls shows inode numbers in the first column. You can see that the symlink has a different inode number while the hardlink has the same. You can also use the stat(1) command:

    $ stat a
      File: 'a'
      Size: 0           Blocks: 0          IO Block: 4096   regular empty file
    Device: 28h/40d Inode: 95905       Links: 2
    [...]
    
    $ stat b
      File: 'b' -> 'a'
      Size: 1           Blocks: 0          IO Block: 4096   symbolic link
    Device: 28h/40d Inode: 96990       Links: 1
    [...]
    

    If you want to do this programmatically, you can use the lstat(2) system call to find information about the symlink itself (its inode number etc.), while stat(2) shows information about the target of the symlink, if it exists. Example in Python:

    >>> import os
    >>> os.stat("b").st_ino
    95905
    >>> os.lstat("b").st_ino
    96990
    

提交回复
热议问题