rename(2)
๐ rename(2) ๐ โ the manual page, but make it slay
okay bestie, sit down, hydrate, because today we're learning the Unix system call that lets you move files around. it's rename(2) from the System Calls Manual and honestly? it's giving main character energy. ๐
โจ NAME โจ
rename, renameat, renameat2 โ change the name or location of a file ๐ฆ
๐ LIBRARY ๐
Standard C library (libc, -lc) โ yeah she lives in libc, of course she does. ๐
๐ธ SYNOPSIS ๐ธ
here's the whole crew, copy these exactly bestie, no edits:
#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
#include <fcntl.h> /* Definition of AT_* constants */
#include <stdio.h>
int renameat(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath);
int renameat2(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath, unsigned int flags);
๐ง Feature Test Macro Requirements for glibc ๐ง
(peep feature_test_macros(7) if you wanna go deeper, iykyk)
renameat():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
renameat2():
_GNU_SOURCE
๐ซ DESCRIPTION ๐ซ
so rename() renames a file, and yes she will also move it between directories if that's what you're asking for. no cap. ๐ซถ
here's the tea that people always get wrong: any other hard links to the file (like the ones you made with link(2)) are totally unaffected. open file descriptors for oldpath are also unaffected. she's not touching those, she's just doing her little rename thing. โจ
now, whether the whole operation actually succeeds depends on a bunch of restrictions โ check ERRORS below for the full list, because there's a lot.
if newpath already exists, it gets atomically replaced, so there's no point in time where another process trying to access newpath will find it missing. iconic behaviour honestly. ๐
but heads up: there will probably be a window where botholdpathandnewpathrefer to the file being renamed. so like, don't be shocked when you see both. ๐
if oldpath and newpath are existing hard links referring to the same file, then rename() does literally nothing, and returns a success status. she said "no notes, we're fine". ๐
if newpath exists but the operation fails for some reason, rename() guarantees to leave an instance of newpath in place. safety first bestie. ๐
oldpath can specify a directory. in that case, newpath must either not exist, or it must specify an empty directory.
if oldpath refers to a symbolic link, the link is renamed. if newpath refers to a symbolic link, the link will be overwritten. ๐ฆ
๐ renameat() ๐
renameat() operates in exactly the same way as rename(), except for the differences listed here. (same vibes, tiny plot twist.)
if the pathname given in oldpath is relative, it's interpreted relative to the directory referred to by the file descriptor olddirfd โ not relative to the current working directory of the calling process, which is what rename() does for a relative pathname. ๐ซถ
if oldpath is relative and olddirfd is the special value AT_FDCWD, then oldpath is interpreted relative to the current working directory of the calling process (literally just like rename()).
if oldpath is absolute, then olddirfd is ignored. periodt.
the interpretation of newpath is exactly as for oldpath, except that a relative pathname is interpreted relative to the directory referred to by the file descriptor newdirfd.
if you're sitting there like "why does renameat() even exist" โ go read openat(2) for an explanation of why she's needed. ๐
๐ renameat2() ๐
renameat2() has an extra flags argument. a renameat2() call with a flags argument of zero is equivalent to renameat(). ๐
the flags argument is a bit mask, consisting of zero or more of these flags:
RENAME_EXCHANGEโ atomically exchangeoldpathandnewpath. both pathnames must exist, but they may be of different types. (like, one could be a non-empty directory and the other a symbolic link. the chaos is allowed. ๐ฅ)RENAME_NOREPLACEโ don't overwritenewpathduring the rename. return an error ifnewpathalready exists.
RENAME_NOREPLACE can't be employed together with RENAME_EXCHANGE. ๐ซ
RENAME_NOREPLACE requires support from the underlying filesystem. support for various filesystems was added as follows:
- ext4 (Linux 3.15);
- btrfs, tmpfs, and cifs (Linux 3.17);
- xfs (Linux 4.0);
- support for many other filesystems was added in Linux 4.9, including ext2, minix, reiserfs, jfs, vfat, and bpf. โจ
RENAME_WHITEOUT(since Linux 3.18) โ this operation makes sense only for overlay/union filesystem implementations. ngl it's the weird one and we love her for it.
specifying RENAME_WHITEOUT creates a "whiteout" object at the source of the rename at the same time as performing the rename. the whole operation is atomic, so if the rename succeeds, the whiteout will also have been created. ๐
a "whiteout" is an object that has special meaning in union/overlay filesystem constructs. in these constructs, multiple layers exist and only the top one is ever modified. a whiteout on an upper layer will effectively hide a matching file in the lower layer, making it appear as if the file didn't exist. ๐
when a file that exists on the lower layer is renamed, the file is first copied up (if it's not already on the upper layer) and then renamed on the upper, read-write layer. at the same time, the source file needs to be "whiteouted" (so that the version of the source file in the lower layer is rendered invisible). the whole operation needs to be done atomically. ๐ฅ
when it's not part of a union/overlay, the whiteout appears as a character device with a {0,0} device number. (note that other union/overlay implementations may employ different methods for storing whiteout entries; specifically, BSD union mount employs a separate inode type, DT_WHT, which, while supported by some filesystems available in Linux, such as CODA and XFS, is ignored by the kernel's whiteout support code, as of Linux 4.19 at least. yeah. ๐)
RENAME_WHITEOUT requires the same privileges as creating a device node โ i.e., the CAP_MKNOD capability. ๐
RENAME_WHITEOUT can't be employed together with RENAME_EXCHANGE. ๐ซ
RENAME_WHITEOUT requires support from the underlying filesystem. among the filesystems that support it are tmpfs (since Linux 3.18), ext4 (since Linux 3.18), XFS (since Linux 4.1), f2fs (since Linux 4.2), btrfs (since Linux 4.7), and ubifs (since Linux 4.9). โจ
๐ RETURN VALUE ๐
on success, zero is returned. slay. ๐
on error, -1 is returned, and errno is set to indicate the error. ๐ญ
๐ ERRORS ๐
okay this is the long part bestie, but we're getting through all of them, no skipping, periodt.
EACCESโ write permission is denied for the directory containingoldpathornewpath; or search permission is denied for one of the directories in the path prefix ofoldpathornewpath; oroldpathis a directory and doesn't allow write permission (which is needed to update the..entry). (see alsopath_resolution(7).)EBUSYโ the rename fails becauseoldpathornewpathis a directory that's in use by some process (maybe as its current working directory, or as its root directory, or because it was open for reading) or is in use by the system (for example as a mount point), while the system considers this an error. (note there's no requirement to returnEBUSYin such cases โ there's nothing wrong with doing the rename anyway โ but it is allowed to returnEBUSYif the system can't otherwise handle such situations.)EDQUOTโ the user's quota of disk blocks on the filesystem has been exhausted. ๐EFAULTโoldpathornewpathpoints outside your accessible address space.EINVALโ the new pathname contained a path prefix of the old; or, more generally, an attempt was made to make a directory a subdirectory of itself. ๐EISDIRโnewpathis an existing directory, butoldpathis not a directory.ELOOPโ too many symbolic links were encountered in resolvingoldpathornewpath.EMLINKโoldpathalready has the maximum number of links to it; or it was a directory and the directory containingnewpathhas the maximum number of links.ENAMETOOLONGโoldpathornewpathwas too long. ๐ญENOENTโ the link named byoldpathdoes not exist; or a directory component innewpathdoes not exist; oroldpathornewpathis an empty string.ENOMEMโ insufficient kernel memory was available.ENOSPCโ the device containing the file has no room for the new directory entry.ENOTDIRโ a component used as a directory inoldpathornewpathis not, in fact, a directory. oroldpathis a directory, andnewpathexists but is not a directory.ENOTEMPTYorEEXISTโnewpathis a nonempty directory, that is, it contains entries other than"."and"..". ๐EPERMorEACCESโ the directory containingoldpathhas the sticky bit (S_ISVTX) set, and the process's effective user ID is neither the user ID of the file to be deleted nor that of the directory containing it, and the process is not privileged (Linux: doesn't have theCAP_FOWNERcapability); ornewpathis an existing file and the directory containing it has the sticky bit set and the process's effective user ID is neither the user ID of the file to be replaced nor that of the directory containing it, and the process is not privileged (Linux: doesn't have theCAP_FOWNERcapability); or the filesystem containingoldpathdoes not support renaming of the type requested.EROFSโ the file is on a read-only filesystem.EXDEVโoldpathandnewpathare not on the same mounted filesystem. (Linux permits a filesystem to be mounted at multiple points, butrename()does not work across different mount points, even if the same filesystem is mounted on both.) ๐ซ
the following additional errors can occur for renameat() and renameat2():
EBADFโoldpath(newpath) is relative butolddirfd(newdirfd) is not a valid file descriptor.ENOTDIRโoldpathis relative andolddirfdis a file descriptor referring to a file other than a directory; or similar fornewpathandnewdirfd.
the following additional errors can occur for renameat2():
EEXISTโflagscontainsRENAME_NOREPLACEandnewpathalready exists.EINVALโ an invalid flag was specified inflags.EINVALโ bothRENAME_NOREPLACEandRENAME_EXCHANGEwere specified inflags.EINVALโ bothRENAME_WHITEOUTandRENAME_EXCHANGEwere specified inflags.EINVALโ the filesystem does not support one of the flags inflags.ENOENTโflagscontainsRENAME_EXCHANGEandnewpathdoes not exist.EPERMโRENAME_WHITEOUTwas specified inflags, but the caller does not have theCAP_MKNODcapability.
๐ STANDARDS ๐
rename()โ C11, POSIX.1-2024. โจrenameat()โ POSIX.1-2024. ๐renameat2()โ Linux. ๐ง
๐ HISTORY ๐
okay so bestie, let's do the lore timeline โจ
rename()โ C89, POSIX.1-2001, 4.3BSD. an absolute elder, been here since forever, iconic behavior honestly ๐renameat()โ POSIX.1-2008. showed up in Linux 2.6.16 and glibc 2.4 like the sequel nobody asked for but everybody needed ๐renameat2()โ Linux 3.15, glibc 2.28. the new girl on the block with the extra flags, we love a plot twist ๐ฅ
๐ง glibc notes ๐ง
ok so glibc is out here doing damage control for older kernels, and it's lowkey impressive:
On older kernels where renameat() is not available, the glibc wrapper function falls back to the use of rename() ๐ซถ. literally covering for its little sibling, so real.
And here's the sneaky part โ when oldpath and newpath are relative pathnames, glibc constructs the pathnames based on the symbolic links in /proc/self/fd that correspond to the olddirfd and newdirfd arguments ๐
. yes, it's doing path math behind your back, it's giving undercover agent energy.
๐ BUGS ๐
bestie I need you to sit down for this one, because it's genuinely the scary part ๐ญ
On NFS filesystems, you can not assume that if the operation failed, the file was not renamed. like. read that again fr fr.
here's the whole tragic sequence:
- the server does the rename operation ๐
- and then the server crashes ๐
- the retransmitted RPC, which will be processed when the server is up again, causes a failure
- so likeโฆ it worked, but it also told you it didn't?? unhinged behavior ngl
โ ๏ธ The application is expected to deal with this. yes, you. it's your problem now, no cap. See link(2) for a similar problem โ same chaotic energy, different syscall ๐ญ
๐ SEE ALSO ๐
the little squad of related pages, all linked and all iconic:
that's the whole page bestie, you ate it ๐
โจ this was Linux man-pages 6.20, dated 2026-02-08, for rename(2) โ go forth and rename things, carefully, and always with a vibe check first ๐ฆ๐
๐ฆ 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-1995, 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