Link function: to create a link to an existing file, use
I personally understand it as a CP command
# Include <unistd. h> int Link (const char * existingpath, const char * newpath); Return Value: 0 if success is returned,-1 if error is returned
This function creates a new directory item newpath, which references the existing file existingpath. If newpath already exists, an error is returned.
Create only the last component in newpath, and other parts in the path should already exist.
Creating a new directory item and adding a link count should be an atomic operation.
Unlink function: deletes an existing Directory item and can call the unlink function.
I personally think it is RM command
# Include <unistd. h> int unlink (const char * pathname); Return Value: If successful, 0 is returned. If an error occurs,-1 is returned.
This function deletes directory items and reduces the link count of the file referenced by pathname by 1.
If there are other links to the file, you can still access the data of the file through other links. If an error occurs, no changes are made to the file.
To unlink a file, you must have the write and execute permissions on the directory containing the directory items. If a sticky bit is set for the directory, you must have the write permission for the Directory and have one of the following three conditions:
Owns the file.
Owns this directory.
Super User privileges.
Instance: A bar exists.
# Include "apue. H"
# Include <fcntl. h>
Int main ()
{
If (open ("bar", o_rdwr) <0)
Err_sys ("Open error ");
If (unlink ("bar") <0)
Err_sys ("unlink error ");
Printf ("file unlink \ n ");
Sleep (15 );
Printf ("done \ n ");
Exit (0 );
}
The remove function unlinks a file or directory,
For files, the remove function is the same as that of Unlink. For directories, the remove function is the same as rmdir.
# Include <stdio. h> int remove (const char * pathname); Return Value: 0 if successful,-1 if error
Rename function rename a file or directory
# Include <stdio. h> int Rename (const char * oldname, const char * newname); Return Value: 0 if successful,-1 if error
Link, unlink, remove, rename Function