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 both oldpath and newpath refer 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 exchange oldpath and newpath. 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 overwrite newpath during the rename. return an error if newpath already 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 containing oldpath or newpath; or search permission is denied for one of the directories in the path prefix of oldpath or newpath; or oldpath is a directory and doesn't allow write permission (which is needed to update the .. entry). (see also path_resolution(7).)
  • EBUSY โ€” the rename fails because oldpath or newpath is 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 return EBUSY in such cases โ€” there's nothing wrong with doing the rename anyway โ€” but it is allowed to return EBUSY if the system can't otherwise handle such situations.)
  • EDQUOT โ€” the user's quota of disk blocks on the filesystem has been exhausted. ๐Ÿ’€
  • EFAULT โ€” oldpath or newpath points 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 โ€” newpath is an existing directory, but oldpath is not a directory.
  • ELOOP โ€” too many symbolic links were encountered in resolving oldpath or newpath.
  • EMLINK โ€” oldpath already has the maximum number of links to it; or it was a directory and the directory containing newpath has the maximum number of links.
  • ENAMETOOLONG โ€” oldpath or newpath was too long. ๐Ÿ˜ญ
  • ENOENT โ€” the link named by oldpath does not exist; or a directory component in newpath does not exist; or oldpath or newpath is 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 in oldpath or newpath is not, in fact, a directory. or oldpath is a directory, and newpath exists but is not a directory.
  • ENOTEMPTY or EEXIST โ€” newpath is a nonempty directory, that is, it contains entries other than "." and "..". ๐Ÿ™ˆ
  • EPERM or EACCES โ€” the directory containing oldpath has 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 the CAP_FOWNER capability); or newpath is 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 the CAP_FOWNER capability); or the filesystem containing oldpath does not support renaming of the type requested.
  • EROFS โ€” the file is on a read-only filesystem.
  • EXDEV โ€” oldpath and newpath are not on the same mounted filesystem. (Linux permits a filesystem to be mounted at multiple points, but rename() 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 but olddirfd (newdirfd) is not a valid file descriptor.
  • ENOTDIR โ€” oldpath is relative and olddirfd is a file descriptor referring to a file other than a directory; or similar for newpath and newdirfd.

the following additional errors can occur for renameat2():

  • EEXIST โ€” flags contains RENAME_NOREPLACE and newpath already exists.
  • EINVAL โ€” an invalid flag was specified in flags.
  • EINVAL โ€” both RENAME_NOREPLACE and RENAME_EXCHANGE were specified in flags.
  • EINVAL โ€” both RENAME_WHITEOUT and RENAME_EXCHANGE were specified in flags.
  • EINVAL โ€” the filesystem does not support one of the flags in flags.
  • ENOENT โ€” flags contains RENAME_EXCHANGE and newpath does not exist.
  • EPERM โ€” RENAME_WHITEOUT was specified in flags, but the caller does not have the CAP_MKNOD capability.

๐Ÿ“œ 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms