unlink(2)
โจ unlink(2) โจ
the unlink and unlinkat system calls: bestie, we're deleting a name (and maybe the file too) ๐
okay so first things first โ this is the unlink(2) page from the Linux system calls manual. Two functions, one mission: making names disappear. no cap. ๐ซถ
๐ธ NAME ๐ธ
unlink, unlinkat โ delete a name and possibly the file it refers to
like... the name goes. the file maybe goes. it's giving "it's complicated" ๐ญ
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ฆ SYNOPSIS ๐ฆ
#include <unistd.h>
int unlink(const char *path);
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
int unlinkat(int dirfd, const char *path, int flags);
โจ Feature Test Macro Requirements for glibc โจ
(peep feature_test_macros(7) if you need the whole lore)
unlinkat():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
๐ DESCRIPTION ๐
ok so unlink() deletes a name from the filesystem. that's the whole vibe. ๐ฅ
if that name was the last link to a file and no processes have the file open โ boom, the file is deleted and the space it was using is made available for reuse. gone. recycled. ๐๏ธโจ
if the name was the last link to a file but some processes still have that file open? the file stays alive until the last file descriptor referring to it is closed. she's not dead, she's just... unreferenced by name. main character energy tbh. ๐
if the name referred to a symbolic link, the link itself is removed.
if the name referred to a socket, FIFO, or device โ the name for it gets removed, but processes which have the object open may continue to use it. so like, the nametag is gone but everybody's still at the party. ๐
๐ unlinkat()
unlinkat() operates in exactly the same way as either unlink() or rmdir(2) โ depending on whether or not flags includes the AT_REMOVEDIR flag โ except for the differences described here. (yes, she's a two-for-one. iconic.)
- if
pathis relative, then it is interpreted relative to the directory referred to by the file descriptordirfdโ rather than relative to the current working directory of the calling process, which is whatunlink()andrmdir(2)do for a relative pathname. - if
pathis relative anddirfdis the special valueAT_FDCWD, thenpathis interpreted relative to the current working directory of the calling process โ just likeunlink()andrmdir(2)do. so basically the default behaviour, bestie. - if
pathis absolute, thendirfdis ignored. sorry dirfd, not your moment. ๐
flags is a bit mask that can either be specified as 0, or by ORing together flag values that control the operation of unlinkat(). currently, only one such flag is defined:
AT_REMOVEDIRโ by default,unlinkat()performs the equivalent ofunlink()onpath. if theAT_REMOVEDIRflag is specified, it performs the equivalent ofrmdir(2)onpath. (so it's a directory deleter now. versatile. we love her.)
peepopenat(2)for an explanation of the need forunlinkat(). iykyk ๐
โญ RETURN VALUE โญ
- on success: zero is returned. slay. ๐
- on error:
-1is returned, anderrnois set to indicate the error. ๐ญ
๐ ERRORS ๐
buckle up bestie, this is the long part. every single one of these matters fr fr.
EACCESโ write access to the directory containingpathis not allowed for the process's effective UID, or one of the directories inpathdid not allow search permission. (see alsopath_resolution(7).)EBUSYโpathcannot be unlinked because it is being used by the system or another process; for example, it is a mount point or the NFS client software created it to represent an active but otherwise nameless inode ("NFS silly renamed"). she's busy, she said not right now. ๐EFAULTโpathpoints outside your accessible address space.EIOโ an I/O error occurred.EISDIRโpathrefers to a directory. (this is the non-POSIX value returned since Linux 2.1.132.)ELOOPโ too many symbolic links were encountered in translatingpath.ENAMETOOLONGโpathwas too long. ๐ญENOENTโ a component inpathdoes not exist or is a dangling symbolic link, orpathis empty.ENOMEMโ insufficient kernel memory was available.ENOTDIRโ a component used as a directory inpathis not, in fact, a directory.EPERMโ the system does not allow unlinking of directories, or unlinking of directories requires privileges that the calling process doesn't have. (this is the POSIX prescribed error return; as noted above, Linux returnsEISDIRfor this case.)EPERM(Linux only) โ the filesystem does not allow unlinking of files.EPERMorEACCESโ the directory containingpathhas the sticky bit (S_ISVTX) set and the process's effective UID is neither the UID of the file to be deleted nor that of the directory containing it, and the process is not privileged (Linux: does not have theCAP_FOWNERcapability).EPERMโ the file to be unlinked is marked immutable or append-only. (seeFS_IOC_SETFLAGS(2const).)EROFSโpathrefers to a file on a read-only filesystem. ๐ง
the same errors that occur for unlink() and rmdir(2) can also occur for unlinkat(). the following additional errors can occur for unlinkat():
EBADFโpathis relative butdirfdis neitherAT_FDCWDnor a valid file descriptor.EINVALโ an invalid flag value was specified inflags.EISDIRโpathrefers to a directory, andAT_REMOVEDIRwas not specified inflags.ENOTDIRโpathis relative anddirfdis a file descriptor referring to a file other than a directory.
๐ STANDARDS ๐
POSIX.1-2024.
๐ง HISTORY ๐ง
unlink()โ SVr4, 4.3BSD, POSIX.1-2001.unlinkat()โ POSIX.1-2008. Linux 2.6.16, glibc 2.4.
๐ glibc ๐
on older kernels where unlinkat() is unavailable, the glibc wrapper function falls back to the use of unlink() or rmdir(2). and when path is relative, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument. resourceful queen. ๐
๐ BUGS ๐
infelicities in the protocol underlying NFS can cause the unexpected disappearance of files which are still being used. so like... your file just vanishes?? while you're using it?? yeah. nfs said "surprise". ๐
๐ SEE ALSO ๐
rm(1), unlink(1), chmod(2), link(2), mknod(2), open(2), rename(2), rmdir(2), mkfifo(3), remove(3), path_resolution(7), symlink(7)
okay that's the whole page bestie, go forth and delete responsibly ๐ซถโจ
this was Linux man-pages 6.20, dated 2026-02-08 โ mwah, kisses, take care of her ๐ ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 1993, Ian Jackson <iwj10@cus.cam.ac.uk>
- Copyright 2006-2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms