Link, unlink, remove, rename Function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.