Linux remove函数和unlink函数

本文详细介绍了Linux系统编程中的unlink和remove命令。这两个命令用于删除文件,其中remove内部调用unlink。当文件没有其他链接或进程打开时,文件会被真正删除。如果仍有进程使用,文件会等到所有引用关闭后才被删除。对于符号链接、socket、FIFO和设备文件,unlink和remove同样会删除名称,但不影响正在使用的对象。成功删除返回0,失败返回-1并设置错误码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用man命令查看这两个命令:

1. unlink

unlink有两个,1类和2类,函数中使用的是2类,因此使用以下命令查看:

$ man 2 unlink
NAME
       unlink, unlinkat - delete a name and possibly the file it refers to

SYNOPSIS
       #include <unistd.h>

       int unlink(const char *pathname);

       #include <fcntl.h>           /* Definition of AT_* constants */
       #include <unistd.h>

       int unlinkat(int dirfd, const char *pathname, int flags);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       unlinkat():
           Since glibc 2.10:
               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
               _ATFILE_SOURCE

DESCRIPTION
       unlink()  deletes  a name from the filesystem.  If that name was the last link to a file and
       no processes have the file open, the file is deleted and the space  it  was  using  is  made
       available for reuse.

       If the name was the last link to a file but any processes still have the file open, the file
       will remain in existence until the last file descriptor referring to it is closed.

       If the name referred to a symbolic link, the link is removed.

       If the name referred to a socket, FIFO, or device, the name for it is removed but  processes
       which have the object open may continue to use it.

 

2. remove

NAME
       remove - remove a file or directory

SYNOPSIS
       #include <stdio.h>

       int remove(const char *pathname);

DESCRIPTION
       remove() deletes a name from the filesystem.  It calls unlink(2) for files, and rmdir(2) for
       directories.

       If the removed name was the last link to a file and no processes have  the  file  open,  the
       file is deleted and the space it was using is made available for reuse.

       If  the  name  was  the last link to a file, but any processes still have the file open, the
       file will remain in existence until the last file descriptor referring to it is closed.

       If the name referred to a symbolic link, the link is removed.

       If the name referred to a socket, FIFO, or device, the name is removed, but processes  which
       have the object open may continue to use it.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       The errors that occur are those for unlink(2) and rmdir(2).

3. 总结

可以看到,当删除文件时,remove调用的就是unlink。

功能:

1. 当文件没有被别的应用打开且它是最后一个指向文件的链接,那么此文件将被删除,它占用的空间将被释放。

2. 当文件虽然是最后一个指向文件的链接,但有其他进程正使用此文件,此文件要到它最后一个文件描述符被关闭,才能被删除。

3. 当文件名指向的是一个符号链接,那么此符号链接被删除。

4. 当文件名指向的是一个 socket / FIFO / device,这个文件名将会删除,但持有此文件对象的进程还是会使用它。

返回值:

成功返回0; 失败返回-1, 并置对应的失败码。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值