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 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Modified Sat Jul 24 00:35:52 1993 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified Thu Jun 4 12:21:13 1998 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Thu Mar 3 09:49:35 2005 by Michael Haardt <michael@moria.de>
31 .\" 2007-03-25, mtk, added various text to DESCRIPTION.
33 .TH RENAME 2 2017-09-15 "Linux" "Linux Programmer's Manual"
35 rename, renameat, renameat2 \- change the name or location of a file
40 .BI "int rename(const char *" oldpath ", const char *" newpath );
42 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
45 .BI "int renameat(int " olddirfd ", const char *" oldpath ,
46 .BI " int " newdirfd ", const char *" newpath );
48 .BI "int renameat2(int " olddirfd ", const char *" oldpath ,
49 .BI " int " newdirfd ", const char *" newpath \
50 ", unsigned int " flags );
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
64 _POSIX_C_SOURCE\ >=\ 200809L
79 renames a file, moving it between directories if required.
80 Any other hard links to the file (as created using
83 Open file descriptors for
87 Various restrictions determine whether or not the rename operation succeeds:
92 already exists, it will be atomically replaced, so that there is
93 no point at which another process attempting to access
96 However, there will probably be a window in which both
100 refer to the file being renamed.
106 are existing hard links referring to the same file, then
108 does nothing, and returns a success status.
112 exists but the operation fails for some reason,
114 guarantees to leave an instance of
119 can specify a directory.
122 must either not exist, or it must specify an empty directory.
126 refers to a symbolic link, the link is renamed; if
128 refers to a symbolic link, the link will be overwritten.
132 system call operates in exactly the same way as
134 except for the differences described here.
136 If the pathname given in
138 is relative, then it is interpreted relative to the directory
139 referred to by the file descriptor
141 (rather than relative to the current working directory of
142 the calling process, as is done by
144 for a relative pathname).
154 is interpreted relative to the current working
155 directory of the calling process (like
164 The interpretation of
168 except that a relative pathname is interpreted relative
169 to the directory referred to by the file descriptor
174 for an explanation of the need for
185 argument is equivalent to
190 argument is a bit mask consisting of zero or more of the following flags:
197 Both pathnames must exist
198 but may be of different types (e.g., one could be a non-empty directory
199 and the other a symbolic link).
210 can't be employed together with
211 .BR RENAME_EXCHANGE .
214 requires support from the underlying filesystem.
215 Support for various filesystems was added as follows:
219 .\" ext4: commit 0a7c3937a1f23f8cb5fc77ae01661e9968a51d0c
221 btrfs, shmem, and cifs (Linux 3.17);
224 .\" btrfs: commit 80ace85c915d0f41016f82917218997b72431258
225 .\" shmem: commit 3b69ff51d087d265aa4af3a532fc4f20bf33e718
226 .\" cifs: commit 7c33d5972ce382bcc506d16235f1e9b7d22cbef8
230 Support for many other filesystems was added in Linux 4.9, including
231 etx2, minix, reiserfs, jfs, vfat, and bpf.
232 .\" Also affs, bfs, exofs, hfs, hfsplus, jffs2, logfs, msdos,
233 .\" nilfs2, omfs, sysvfs, ubifs, udf, ufs
235 .\" local filesystems: commit f03b8ad8d38634d13e802165cc15917481b47835
236 .\" libfs: commit e0e0be8a835520e2f7c89f214dfda570922a1b90
239 .BR RENAME_WHITEOUT " (since Linux 3.18)"
240 .\" commit 0d7a855526dd672e114aff2ac22b60fc6f155b08
241 .\" commit 787fb6bc9682ec7c05fb5d9561b57100fbc1cc41
242 This operation makes sense only for overlay/union
243 filesystem implementations.
247 creates a "whiteout" object at the source of
248 the rename at the same time as performing the rename.
249 The whole operation is atomic,
250 so that if the rename succeeds then the whiteout will also have been created.
252 A "whiteout" is an object that has special meaning in union/overlay
253 filesystem constructs.
255 multiple layers exist and only the top one is ever modified.
256 A whiteout on an upper layer will effectively hide a
257 matching file in the lower layer,
258 making it appear as if the file didn't exist.
260 When a file that exists on the lower layer is renamed,
261 the file is first copied up (if not already on the upper layer)
262 and then renamed on the upper, read-write layer.
263 At the same time, the source file needs to be "whiteouted"
264 (so that the version of the source file in the lower layer
265 is rendered invisible).
266 The whole operation needs to be done atomically.
268 When not part of a union/overlay,
269 the whiteout appears as a character device with a {0,0} device number.
272 requires the same privileges as creating a device node (i.e., the
277 can't be employed together with
278 .BR RENAME_EXCHANGE .
281 requires support from the underlying filesystem.
282 Among the filesystems that provide that support are
283 tmpfs (since Linux 3.18),
284 .\" tmpfs: commit 46fdb794e3f52ef18b859ebc92f0a9d7db21c5df
285 ext4 (since Linux 3.18),
286 .\" ext4: commit cd808deced431b66b5fa4e5c193cb7ec0059eaff
287 and XFS (since Linux 4.1).
288 .\" XFS: commit 7dcf5c3e4527cfa2807567b00387cf2ed5e07f00
290 On success, zero is returned.
291 On error, \-1 is returned, and
293 is set appropriately.
297 Write permission is denied for the directory containing
301 or, search permission is denied for one of the directories
302 in the path prefix of
308 is a directory and does not allow write permission (needed to update
313 .BR path_resolution (7).)
316 The rename fails because
317 .IR oldpath " or " newpath
318 is a directory that is in use by some process (perhaps as
319 current working directory, or as root directory, or because
320 it was open for reading) or is in use by the system
321 (for example as mount point), while the system considers
323 (Note that there is no requirement to return
326 cases\(emthere is nothing wrong with doing the rename anyway\(embut
327 it is allowed to return
329 if the system cannot otherwise
330 handle such situations.)
333 The user's quota of disk blocks on the filesystem has been exhausted.
336 .IR oldpath " or " newpath " points outside your accessible address space."
339 The new pathname contained a path prefix of the old, or, more generally,
340 an attempt was made to make a directory a subdirectory of itself.
344 is an existing directory, but
349 Too many symbolic links were encountered in resolving
350 .IR oldpath " or " newpath .
354 already has the maximum number of links to it, or
355 it was a directory and the directory containing
357 has the maximum number of links.
360 .IR oldpath " or " newpath " was too long."
366 or, a directory component in
376 Insufficient kernel memory was available.
379 The device containing the file has no room for the new directory
383 A component used as a directory in
384 .IR oldpath " or " newpath
385 is not, in fact, a directory.
390 exists but is not a directory.
392 .BR ENOTEMPTY " or " EEXIST
394 is a nonempty directory, that is, contains entries other than "." and "..".
396 .BR EPERM " or " EACCES
397 The directory containing
401 set and the process's effective user ID is neither
402 the user ID of the file to be deleted nor that of the directory
403 containing it, and the process is not privileged
404 (Linux: does not have the
409 is an existing file and the directory containing it has the sticky bit set
410 and the process's effective user ID is neither the user ID of the file
411 to be replaced nor that of the directory containing it,
412 and the process is not privileged
413 (Linux: does not have the
416 or the filesystem containing
418 does not support renaming of the type requested.
421 The file is on a read-only filesystem.
424 .IR oldpath " and " newpath
425 are not on the same mounted filesystem.
426 (Linux permits a filesystem to be mounted at multiple points, but
428 does not work across different mount points,
429 even if the same filesystem is mounted on both.)
431 The following additional errors can occur for
440 is not a valid file descriptor.
446 is a file descriptor referring to a file other than a directory;
452 The following additional errors can occur for
464 An invalid flag was specified in
484 The filesystem does not support one of the flags in
499 but the caller does not have the
504 was added to Linux in kernel 2.6.16;
505 library support was added to glibc in version 2.4.
508 was added to Linux in kernel 3.15; library support was added in glibc 2.28.
511 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
521 On older kernels where
523 is unavailable, the glibc wrapper function falls back to the use of
529 are relative pathnames,
530 glibc constructs pathnames based on the symbolic links in
532 that correspond to the
538 On NFS filesystems, you can not assume that if the operation
539 failed, the file was not renamed.
540 If the server does the rename operation
541 and then crashes, the retransmitted RPC which will be processed when the
542 server is up again causes a failure.
543 The application is expected to
547 for a similar problem.
554 .BR path_resolution (7),