Moving Files

The mv(1) command uses the link(2) and unlink(2) calls in order to move a file. However, if the file is moved to another file system, the mv(1) command must copy it. Assuming that the file is being moved within the same file system, an example command looks like this:

$ mv ./a.out ./bin/my_app
					

In C terms, this is accomplished as follows:

if ( link("./a.out","./bin/my_app") == -1 ) {
    fprintf(stderr,"%s: link(2)\n",strerror(errno));
    abort();
}
if ( unlink("./a.out") == -1 ) {
    fprintf(stderr,"%s: unlink(2)\n",strerror(errno));
    abort();
}

The idea behind moving a file is to create a new link and then remove the old link. This gives the illusion of moving the file from one path to another. However, if the source and destination pathnames ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.