2009年5月6日星期三

Linux hard link

You may confused what the hard link is, and why the delete file use the unlink(2) system call. this example will help you somehow.


assumption, there is a file in /tmp/a.txt

$ cat /tmp/a.txt
hello, i'm a.txt

then, we write this test.c
test.c:
#include
int main(void)
{
link("/tmp/a.txt", "/tmp/new_a.txt");
/* in this moment, the a.txt have same content with new_a.txt */
unlink("/tmp/a.txt");
/* in this moment, a.txt will be delete */
}

we compile the test.c
$gcc test.c

and run it
$./a.out

$ cat /tmp/new_a.txt
hello, i'm a.txt

Use hard link is the simplest and the fastest way move files within the same filesystem, since hard link cann't across filesystem.

More infomation you need check the link(2) or the unix filesystem design.

没有评论: