1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt;
3 .\" and Copyright (C) 1993,1995 Ian Jackson
4 .\" and Copyright (C) 2006, 2014 Michael Kerrisk
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" Modified Sat Jul 24 00:35:52 1993 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified Thu Jun 4 12:21:13 1998 by Andries Brouwer <aeb@cwi.nl>
10 .\" Modified Thu Mar 3 09:49:35 2005 by Michael Haardt <michael@moria.de>
11 .\" 2007-03-25, mtk, added various text to DESCRIPTION.
13 .TH rename 2 (date) "Linux man-pages (unreleased)"
15 rename, renameat, renameat2 \- change the name or location of a file
18 .RI ( libc ", " \-lc )
23 .BI "int rename(const char *" oldpath ", const char *" newpath );
25 .BR "#include <fcntl.h> " "/* Definition of " AT_* " constants */"
28 .BI "int renameat(int " olddirfd ", const char *" oldpath ,
29 .BI " int " newdirfd ", const char *" newpath );
30 .BI "int renameat2(int " olddirfd ", const char *" oldpath ,
31 .BI " int " newdirfd ", const char *" newpath \
32 ", unsigned int " flags );
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
43 _POSIX_C_SOURCE >= 200809L
52 renames a file, moving it between directories if required.
53 Any other hard links to the file (as created using
56 Open file descriptors for
60 Various restrictions determine whether or not the rename operation succeeds:
65 already exists, it will be atomically replaced, so that there is
66 no point at which another process attempting to access
69 However, there will probably be a window in which both
73 refer to the file being renamed.
79 are existing hard links referring to the same file, then
81 does nothing, and returns a success status.
85 exists but the operation fails for some reason,
87 guarantees to leave an instance of
92 can specify a directory.
95 must either not exist, or it must specify an empty directory.
99 refers to a symbolic link, the link is renamed; if
101 refers to a symbolic link, the link will be overwritten.
105 system call operates in exactly the same way as
107 except for the differences described here.
109 If the pathname given in
111 is relative, then it is interpreted relative to the directory
112 referred to by the file descriptor
114 (rather than relative to the current working directory of
115 the calling process, as is done by
117 for a relative pathname).
127 is interpreted relative to the current working
128 directory of the calling process (like
137 The interpretation of
141 except that a relative pathname is interpreted relative
142 to the directory referred to by the file descriptor
147 for an explanation of the need for
158 argument is equivalent to
163 argument is a bit mask consisting of zero or more of the following flags:
170 Both pathnames must exist
171 but may be of different types (e.g., one could be a non-empty directory
172 and the other a symbolic link).
183 can't be employed together with
184 .BR RENAME_EXCHANGE .
187 requires support from the underlying filesystem.
188 Support for various filesystems was added as follows:
192 .\" ext4: commit 0a7c3937a1f23f8cb5fc77ae01661e9968a51d0c
194 btrfs, tmpfs, and cifs (Linux 3.17);
197 .\" btrfs: commit 80ace85c915d0f41016f82917218997b72431258
198 .\" tmpfs: commit 3b69ff51d087d265aa4af3a532fc4f20bf33e718
199 .\" cifs: commit 7c33d5972ce382bcc506d16235f1e9b7d22cbef8
201 .\" gfs2 in Linux 4.2?
203 Support for many other filesystems was added in Linux 4.9, including
204 ext2, minix, reiserfs, jfs, vfat, and bpf.
205 .\" Also affs, bfs, exofs, hfs, hfsplus, jffs2, logfs, msdos,
206 .\" nilfs2, omfs, sysvfs, ubifs, udf, ufs
208 .\" local filesystems: commit f03b8ad8d38634d13e802165cc15917481b47835
209 .\" libfs: commit e0e0be8a835520e2f7c89f214dfda570922a1b90
212 .BR RENAME_WHITEOUT " (since Linux 3.18)"
213 .\" commit 0d7a855526dd672e114aff2ac22b60fc6f155b08
214 .\" commit 787fb6bc9682ec7c05fb5d9561b57100fbc1cc41
215 This operation makes sense only for overlay/union
216 filesystem implementations.
220 creates a "whiteout" object at the source of
221 the rename at the same time as performing the rename.
222 The whole operation is atomic,
223 so that if the rename succeeds then the whiteout will also have been created.
225 A "whiteout" is an object that has special meaning in union/overlay
226 filesystem constructs.
228 multiple layers exist and only the top one is ever modified.
229 A whiteout on an upper layer will effectively hide a
230 matching file in the lower layer,
231 making it appear as if the file didn't exist.
233 When a file that exists on the lower layer is renamed,
234 the file is first copied up (if not already on the upper layer)
235 and then renamed on the upper, read-write layer.
236 At the same time, the source file needs to be "whiteouted"
237 (so that the version of the source file in the lower layer
238 is rendered invisible).
239 The whole operation needs to be done atomically.
241 When not part of a union/overlay,
242 the whiteout appears as a character device with a {0,0} device number.
243 .\" https://www.freebsd.org/cgi/man.cgi?query=mount_unionfs&manpath=FreeBSD+11.0-RELEASE
244 (Note that other union/overlay implementations may employ different methods
245 for storing whiteout entries; specifically, BSD union mount employs
246 a separate inode type,
248 which, while supported by some filesystems available in Linux,
249 such as CODA and XFS, is ignored by the kernel's whiteout support code,
250 as of Linux 4.19, at least.)
253 requires the same privileges as creating a device node (i.e., the
258 can't be employed together with
259 .BR RENAME_EXCHANGE .
262 requires support from the underlying filesystem.
263 Among the filesystems that support it are
264 tmpfs (since Linux 3.18),
265 .\" tmpfs: commit 46fdb794e3f52ef18b859ebc92f0a9d7db21c5df
266 ext4 (since Linux 3.18),
267 .\" ext4: commit cd808deced431b66b5fa4e5c193cb7ec0059eaff
268 XFS (since Linux 4.1),
269 .\" XFS: commit 7dcf5c3e4527cfa2807567b00387cf2ed5e07f00
270 f2fs (since Linux 4.2),
271 .\" f2fs: commit 7e01e7ad746bc8198a8b46163ddc73a1c7d22339
272 btrfs (since Linux 4.7),
273 .\" btrfs: commit cdd1fedf8261cd7a73c0596298902ff4f0f04492
274 and ubifs (since Linux 4.9).
275 .\" ubifs: commit 9e0a1fff8db56eaaebb74b4a3ef65f86811c4798
277 On success, zero is returned.
278 On error, \-1 is returned, and
280 is set to indicate the error.
284 Write permission is denied for the directory containing
288 or, search permission is denied for one of the directories
289 in the path prefix of
295 is a directory and does not allow write permission (needed to update
300 .BR path_resolution (7).)
303 The rename fails because
304 .IR oldpath " or " newpath
305 is a directory that is in use by some process (perhaps as
306 current working directory, or as root directory, or because
307 it was open for reading) or is in use by the system
308 (for example as a mount point), while the system considers
310 (Note that there is no requirement to return
313 cases\(emthere is nothing wrong with doing the rename anyway\(embut
314 it is allowed to return
316 if the system cannot otherwise
317 handle such situations.)
320 The user's quota of disk blocks on the filesystem has been exhausted.
323 .IR oldpath " or " newpath " points outside your accessible address space."
326 The new pathname contained a path prefix of the old, or, more generally,
327 an attempt was made to make a directory a subdirectory of itself.
331 is an existing directory, but
336 Too many symbolic links were encountered in resolving
337 .IR oldpath " or " newpath .
341 already has the maximum number of links to it, or
342 it was a directory and the directory containing
344 has the maximum number of links.
347 .IR oldpath " or " newpath " was too long."
353 or, a directory component in
363 Insufficient kernel memory was available.
366 The device containing the file has no room for the new directory
370 A component used as a directory in
371 .IR oldpath " or " newpath
372 is not, in fact, a directory.
377 exists but is not a directory.
379 .BR ENOTEMPTY " or " EEXIST
381 is a nonempty directory, that is, contains entries other than "." and "..".
383 .BR EPERM " or " EACCES
384 The directory containing
388 set and the process's effective user ID is neither
389 the user ID of the file to be deleted nor that of the directory
390 containing it, and the process is not privileged
391 (Linux: does not have the
396 is an existing file and the directory containing it has the sticky bit set
397 and the process's effective user ID is neither the user ID of the file
398 to be replaced nor that of the directory containing it,
399 and the process is not privileged
400 (Linux: does not have the
403 or the filesystem containing
405 does not support renaming of the type requested.
408 The file is on a read-only filesystem.
411 .IR oldpath " and " newpath
412 are not on the same mounted filesystem.
413 (Linux permits a filesystem to be mounted at multiple points, but
415 does not work across different mount points,
416 even if the same filesystem is mounted on both.)
418 The following additional errors can occur for
429 is not a valid file descriptor.
435 is a file descriptor referring to a file other than a directory;
441 The following additional errors can occur for
453 An invalid flag was specified in
473 The filesystem does not support one of the flags in
488 but the caller does not have the
493 was added in Linux 2.6.16;
494 library support was added in glibc 2.4.
497 was added in Linux 3.15; library support was added in glibc 2.28.
500 4.3BSD, C99, POSIX.1-2001, POSIX.1-2008.
510 On older kernels where
512 is unavailable, the glibc wrapper function falls back to the use of
518 are relative pathnames,
519 glibc constructs pathnames based on the symbolic links in
521 that correspond to the
527 On NFS filesystems, you can not assume that if the operation
528 failed, the file was not renamed.
529 If the server does the rename operation
530 and then crashes, the retransmitted RPC which will be processed when the
531 server is up again causes a failure.
532 The application is expected to
536 for a similar problem.
544 .BR path_resolution (7),