硬连接(hard link)和软连接(symbolic link)

来源:互联网 发布:网络语言酱紫什么意思 编辑:程序博客网 时间:2024/05/28 23:22
stat - display file or file system statusstruct stat {    mode_t       st_mode;    /* file type & mode (permissions) */    ino_t        st_ino;     /* i-node number (serial number) */    dev_t        st_dev;     /* device number (file system) */    dev_t        st_rdev;    /* device number for special files */    nlink_t      st_nlink;    /* number of links */    uid_t        st_uid;     /* user ID of owner */    gid_t        st_gid;     /* group ID of owner */    off_t        st_size;    /* size in bytes, for regular files */    struct timespec st_atim;    /* time of last access */    struct timespec st_mtim;    /* time of last modification */    struct timespec st_ctim;    /* time of last file status change */    blksize_t     st_blksize;   /* best I/O block size */    blkcnt_t      st_blocks;   /* number of disk blocks allocated */};struct dirent{    __ino64_t d_ino;    __off64_t d_off;    unsigned short int d_reclen;    unsigned char d_type;    char d_name[256];       /* We must not include limits.h! */};
ln - make links between files
硬连接(hard link)和软连接(symbolic link)。
link, linkat - make a new name for a file
link()  creates a new link (also known as a hard link) to an existing file.
symlink, symlinkat - make a new name for a file
symlink() creates a symbolic link named linkpath which contains the string target.
unlink, unlinkat - delete a name and possibly the file it refers to
This property of unlink is often used by a program to ensure that a temporary file it creates won’t be left around in case the program crashes. 

Hard Links: 为已存在的文件,新建一个指向该文件inode节点的目录项(directory entry),同时增加该inode的连接数。可以让一个文件有多个硬链接,出现在多个目录下。
Hard link一般有两个限制:
1 不能跨文件系统(不同的文件系统有不同的inode table); 
2 不能连接目录(容易在文件系统中造成循环引用)。
Most implementations require that both pathnames be on the same file system, although POSIX.1 allows an implementation to support linking across file systems. If an implementation supports the creation of hard links to directories, it is restricted to only the superuser. This constraint exists because such hard links can cause loops in the file system, which most utilities that process the file system aren’t capable of handling. Many file system implementations disallow hard links to directories for this reason.
1)Hard links normally require that the link and the file reside in the same file system.
2)Only the superuser can create a hard link to a directory (when supported by the underlying file system).

Symbolic Links:跟hard link不同,它是建立一个独立的文件,而这个文件的作用是当读取这个连接文件时,它会把读取的行为转发到该文件所link的文件上。这样讲,也许比较绕口,那么就来举一个例子。现在有文件a,我们做了一个软连接文件b(只是一个连接文件,非常小),b指向了文件a。当读取b时,那么b就会把读取的动作转发到a上,这样就读取到了文件a。所以,当你删除文件a时,文件b并不会被删除,但是再读取b时,会提示无法打开文件。而当你删除b时,a是不会有任何影响的。

Symbolic links (also called symlinks or softlinks) most resemble Windows shortcuts. They contain a pathname to a target file.

http://www.linuxclues.com/articles/17.htm

17. Hard Links and Symbolic Links
Today we're going to test your virtual imagination ability! You're probably familiar with shortcuts in Microsoft Windows or aliases on the Mac. Linux has something, or actually some things similar, called hard links and symbolic links.

Symbolic links (also called symlinks or softlinks) most resemble Windows shortcuts. They contain a pathname to a target file. Hard links are a bit different. They are listings that contain information about the file. Linux files don't actually live in directories. They are assigned an inode number, which Linux uses to locate files. So a file can have multiple hardlinks, appearing in multiple directories, but isn't deleted until there are no remaining hardlinks to it. Here are some other differences between hardlinks and symlinks:

1. You cannot create a hardlink for a directory.
2. If you remove the original file of a hardlink, the link will still show you the content of the file.
3. A symlink can link to a directory.
4. A symlink, like a Windows shortcut, becomes useless when you remove the original file.

So far this is probably a bit tough to grasp, but stick around, we're going to explain it.

-----------------------

IMPORTANT: The tips in this document require the use of command-line commands. For more information about how to read and execute Linux command-line prompts and commands, please check the Linux Clues Linux Cheat Sheet, especially Linux Prompt Basics and Linux Command-Line Nomenclature. You'll need to start by logging in as root. If you're not sure how to do that, read Logging in and out as Root.

-----------------------

Hardlinks
Let's do a little experiment to demonstrate the case. Make a new directory called Test and then move into it. to do that, type:

$ mkdir Test
$ cd Test

Then make a file called FileA:

$ vi FileA

Press the I key to enter Insert mode:

i

Then type in some funny lines of text (like "Why did the chicken cross the road?") and save the file by typing:

Esc
ZZ

So, you made a file called FileA in a new directory called "Test" in your /home. It contains an old and maybe not so funny joke. Now, let's make a hardlink to FileA. We'll call the hardlink FileB.

$ ln FileA FileB

Then use the "i" argument to list the inodes for both FileA and its hardlink. Type:

$ ls -il FileA FileB

This is what you get:

1482256 -rw-r--r--     2 bruno bruno     21 May 5 15:55 FileA
1482256 -rw-r--r--     2 bruno bruno     21 May 5 15:55 FileB

You can see that both FileA and FileB have the same inode number (1482256). Also both files have the same file permissions and the same size. Because that size is reported for the same inode, it does not consume any extra space on your HD!

Next, remove the original FileA:

$ rm FileA

And have a look at the content of the "link" FileB:

$ cat FileB

You will still be able to read the funny line of text you typed. Hardlinks are cool.

Symlinks
Staying in the same test directory as above, let's make a symlink to FileB. Call the symlink FileC:

$ ln -s FileB FileC

Then use the i argument again to list the inodes.

$ ls -il FileB FileC

This is what you'll get:

1482256 -rw-r--r--     1 bruno bruno     21 May 5 15:55 FileB
1482226 lrwxrwxrwx    1 bruno bruno      5 May 5 16:22 FileC -> FileB

You'll notice the inodes are different and the symlink got a "l" before the rwxrwxrwx. The link has different permissions than the original file because it is just a symbolic link. Its real content is just a string pointing to the original file. The size of the symlink (5) is the size of its string. (The "-> FileB" at the end shows you where the link points to.)

Now list the contents:

$ cat FileB
$ cat FileC

They will show the same funny text.

Now if we remove the original file:

$ rm FileB

and check the Test directory:

$ ls

You'll see the symlink FileC is still there, but if you try to list the contents:

$ cat FileC

It will tell you that there is no such file or directory. You can still list the inode. Typing:

$ ls -il FileC

will still give you:

1482226 lrwxrwxrwx     1 bruno bruno     5 May 5 16:22 FileC -> FileB

But the symlink is obsolete because the original file was removed, as were all the hard links. So the file was deleted even though the symlink remains. (Hope you're still following.)

OK. The test is over, so you can delete the Test directory:

$ cd .. 
$ rm -rf Test     (r stands for recursive and f is for force)


说明:内容主要来自APUE

0 0