close_range.2: Glibc added a wrapper recently
[man-pages.git] / man2 / rename.2
blobdfde1ce6d3d35092015481ebb95cfddce6cb2242
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
5 .\"
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.
10 .\"
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.
15 .\"
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
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
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.
32 .\"
33 .TH RENAME 2 2021-03-22 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 rename, renameat, renameat2 \- change the name or location of a file
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdio.h>
39 .PP
40 .BI "int rename(const char *" oldpath ", const char *" newpath );
41 .PP
42 .BR "#include <fcntl.h>           " "/* Definition of " AT_* " constants */"
43 .B #include <stdio.h>
44 .PP
45 .BI "int renameat(int " olddirfd ", const char *" oldpath ,
46 .BI "             int " newdirfd ", const char *" newpath );
47 .BI "int renameat2(int " olddirfd ", const char *" oldpath ,
48 .BI "             int " newdirfd ", const char *" newpath \
49 ", unsigned int " flags );
50 .fi
51 .PP
52 .RS -4
53 Feature Test Macro Requirements for glibc (see
54 .BR feature_test_macros (7)):
55 .RE
56 .PP
57 .nf
58 .BR renameat ():
59     Since glibc 2.10:
60         _POSIX_C_SOURCE >= 200809L
61     Before glibc 2.10:
62         _ATFILE_SOURCE
63 .PP
64 .BR renameat2 ():
65     _GNU_SOURCE
66 .fi
67 .SH DESCRIPTION
68 .BR rename ()
69 renames a file, moving it between directories if required.
70 Any other hard links to the file (as created using
71 .BR link (2))
72 are unaffected.
73 Open file descriptors for
74 .I oldpath
75 are also unaffected.
76 .PP
77 Various restrictions determine whether or not the rename operation succeeds:
78 see ERRORS below.
79 .PP
81 .I newpath
82 already exists, it will be atomically replaced, so that there is
83 no point at which another process attempting to access
84 .I newpath
85 will find it missing.
86 However, there will probably be a window in which both
87 .I oldpath
88 and
89 .I newpath
90 refer to the file being renamed.
91 .PP
93 .I oldpath
94 and
95 .I newpath
96 are existing hard links referring to the same file, then
97 .BR rename ()
98 does nothing, and returns a success status.
99 .PP
101 .I newpath
102 exists but the operation fails for some reason,
103 .BR rename ()
104 guarantees to leave an instance of
105 .I newpath
106 in place.
108 .I oldpath
109 can specify a directory.
110 In this case,
111 .I newpath
112 must either not exist, or it must specify an empty directory.
115 .I oldpath
116 refers to a symbolic link, the link is renamed; if
117 .I newpath
118 refers to a symbolic link, the link will be overwritten.
119 .SS renameat()
121 .BR renameat ()
122 system call operates in exactly the same way as
123 .BR rename (),
124 except for the differences described here.
126 If the pathname given in
127 .I oldpath
128 is relative, then it is interpreted relative to the directory
129 referred to by the file descriptor
130 .I olddirfd
131 (rather than relative to the current working directory of
132 the calling process, as is done by
133 .BR rename ()
134 for a relative pathname).
137 .I oldpath
138 is relative and
139 .I olddirfd
140 is the special value
141 .BR AT_FDCWD ,
142 then
143 .I oldpath
144 is interpreted relative to the current working
145 directory of the calling process (like
146 .BR rename ()).
149 .I oldpath
150 is absolute, then
151 .I olddirfd
152 is ignored.
154 The interpretation of
155 .I newpath
156 is as for
157 .IR oldpath ,
158 except that a relative pathname is interpreted relative
159 to the directory referred to by the file descriptor
160 .IR newdirfd .
163 .BR openat (2)
164 for an explanation of the need for
165 .BR renameat ().
166 .SS renameat2()
167 .BR renameat2 ()
168 has an additional
169 .I flags
170 argument.
172 .BR renameat2 ()
173 call with a zero
174 .I flags
175 argument is equivalent to
176 .BR renameat ().
179 .I flags
180 argument is a bit mask consisting of zero or more of the following flags:
182 .B RENAME_EXCHANGE
183 Atomically exchange
184 .IR oldpath
186 .IR newpath .
187 Both pathnames must exist
188 but may be of different types (e.g., one could be a non-empty directory
189 and the other a symbolic link).
191 .B RENAME_NOREPLACE
192 Don't overwrite
193 .IR newpath
194 of the rename.
195 Return an error if
196 .IR newpath
197 already exists.
199 .B RENAME_NOREPLACE
200 can't be employed together with
201 .BR RENAME_EXCHANGE .
203 .B RENAME_NOREPLACE
204 requires support from the underlying filesystem.
205 Support for various filesystems was added as follows:
207 .IP * 3
208 ext4 (Linux 3.15);
209 .\" ext4: commit 0a7c3937a1f23f8cb5fc77ae01661e9968a51d0c
210 .IP *
211 btrfs, tmpfs, and cifs (Linux 3.17);
212 .IP *
213 xfs (Linux 4.0);
214 .\" btrfs: commit 80ace85c915d0f41016f82917218997b72431258
215 .\" tmpfs: commit 3b69ff51d087d265aa4af3a532fc4f20bf33e718
216 .\" cifs: commit 7c33d5972ce382bcc506d16235f1e9b7d22cbef8
218 .\" gfs2 in 4.2?
219 .IP *
220 Support for many other filesystems was added in Linux 4.9, including
221 ext2, minix, reiserfs, jfs, vfat, and bpf.
222 .\" Also affs, bfs, exofs, hfs, hfsplus, jffs2, logfs, msdos,
223 .\" nilfs2, omfs, sysvfs, ubifs, udf, ufs
224 .\" hugetlbfs, ramfs
225 .\" local filesystems: commit f03b8ad8d38634d13e802165cc15917481b47835
226 .\" libfs: commit e0e0be8a835520e2f7c89f214dfda570922a1b90
229 .BR RENAME_WHITEOUT " (since Linux 3.18)"
230 .\" commit 0d7a855526dd672e114aff2ac22b60fc6f155b08
231 .\" commit 787fb6bc9682ec7c05fb5d9561b57100fbc1cc41
232 This operation makes sense only for overlay/union
233 filesystem implementations.
235 Specifying
236 .B RENAME_WHITEOUT
237 creates a "whiteout" object at the source of
238 the rename at the same time as performing the rename.
239 The whole operation is atomic,
240 so that if the rename succeeds then the whiteout will also have been created.
242 A "whiteout" is an object that has special meaning in union/overlay
243 filesystem constructs.
244 In these constructs,
245 multiple layers exist and only the top one is ever modified.
246 A whiteout on an upper layer will effectively hide a
247 matching file in the lower layer,
248 making it appear as if the file didn't exist.
250 When a file that exists on the lower layer is renamed,
251 the file is first copied up (if not already on the upper layer)
252 and then renamed on the upper, read-write layer.
253 At the same time, the source file needs to be "whiteouted"
254 (so that the version of the source file in the lower layer
255 is rendered invisible).
256 The whole operation needs to be done atomically.
258 When not part of a union/overlay,
259 the whiteout appears as a character device with a {0,0} device number.
260 .\" https://www.freebsd.org/cgi/man.cgi?query=mount_unionfs&manpath=FreeBSD+11.0-RELEASE
261 (Note that other union/overlay implementations may employ different methods
262 for storing whiteout entries; specifically, BSD union mount employs
263 a separate inode type,
264 .BR DT_WHT ,
265 which, while supported by some filesystems available in Linux,
266 such as CODA and XFS, is ignored by the kernel's whiteout support code,
267 as of Linux 4.19, at least.)
269 .B RENAME_WHITEOUT
270 requires the same privileges as creating a device node (i.e., the
271 .BR CAP_MKNOD
272 capability).
274 .B RENAME_WHITEOUT
275 can't be employed together with
276 .BR RENAME_EXCHANGE .
278 .B RENAME_WHITEOUT
279 requires support from the underlying filesystem.
280 Among the filesystems that support it are
281 tmpfs (since Linux 3.18),
282 .\" tmpfs: commit 46fdb794e3f52ef18b859ebc92f0a9d7db21c5df
283 ext4 (since Linux 3.18),
284 .\" ext4: commit cd808deced431b66b5fa4e5c193cb7ec0059eaff
285 XFS (since Linux 4.1),
286 .\" XFS: commit 7dcf5c3e4527cfa2807567b00387cf2ed5e07f00
287 f2fs (since Linux 4.2),
288 .\" f2fs: commit 7e01e7ad746bc8198a8b46163ddc73a1c7d22339
289 btrfs (since Linux 4.7),
290 .\" btrfs: commit cdd1fedf8261cd7a73c0596298902ff4f0f04492
291 and ubifs (since Linux 4.9).
292 .\" ubifs: commit 9e0a1fff8db56eaaebb74b4a3ef65f86811c4798
293 .SH RETURN VALUE
294 On success, zero is returned.
295 On error, \-1 is returned, and
296 .I errno
297 is set to indicate the error.
298 .SH ERRORS
300 .B EACCES
301 Write permission is denied for the directory containing
302 .I oldpath
304 .IR newpath ,
305 or, search permission is denied for one of the directories
306 in the path prefix of
307 .I oldpath
309 .IR newpath ,
311 .I oldpath
312 is a directory and does not allow write permission (needed to update
314 .I ..
315 entry).
316 (See also
317 .BR path_resolution (7).)
319 .B EBUSY
320 The rename fails because
321 .IR oldpath " or " newpath
322 is a directory that is in use by some process (perhaps as
323 current working directory, or as root directory, or because
324 it was open for reading) or is in use by the system
325 (for example as mount point), while the system considers
326 this an error.
327 (Note that there is no requirement to return
328 .B EBUSY
329 in such
330 cases\(emthere is nothing wrong with doing the rename anyway\(embut
331 it is allowed to return
332 .B EBUSY
333 if the system cannot otherwise
334 handle such situations.)
336 .B EDQUOT
337 The user's quota of disk blocks on the filesystem has been exhausted.
339 .B EFAULT
340 .IR oldpath " or " newpath " points outside your accessible address space."
342 .B EINVAL
343 The new pathname contained a path prefix of the old, or, more generally,
344 an attempt was made to make a directory a subdirectory of itself.
346 .B EISDIR
347 .I newpath
348 is an existing directory, but
349 .I oldpath
350 is not a directory.
352 .B ELOOP
353 Too many symbolic links were encountered in resolving
354 .IR oldpath " or " newpath .
356 .B EMLINK
357 .I oldpath
358 already has the maximum number of links to it, or
359 it was a directory and the directory containing
360 .I newpath
361 has the maximum number of links.
363 .B ENAMETOOLONG
364 .IR oldpath " or " newpath " was too long."
366 .B ENOENT
367 The link named by
368 .I oldpath
369 does not exist;
370 or, a directory component in
371 .I newpath
372 does not exist;
374 .I oldpath
376 .I newpath
377 is an empty string.
379 .B ENOMEM
380 Insufficient kernel memory was available.
382 .B ENOSPC
383 The device containing the file has no room for the new directory
384 entry.
386 .B ENOTDIR
387 A component used as a directory in
388 .IR oldpath " or " newpath
389 is not, in fact, a directory.
391 .I oldpath
392 is a directory, and
393 .I newpath
394 exists but is not a directory.
396 .BR ENOTEMPTY " or " EEXIST
397 .I newpath
398 is a nonempty directory, that is, contains entries other than "." and "..".
400 .BR EPERM " or " EACCES
401 The directory containing
402 .I oldpath
403 has the sticky bit
404 .RB ( S_ISVTX )
405 set and the process's effective user ID is neither
406 the user ID of the file to be deleted nor that of the directory
407 containing it, and the process is not privileged
408 (Linux: does not have the
409 .B CAP_FOWNER
410 capability);
412 .I newpath
413 is an existing file and the directory containing it has the sticky bit set
414 and the process's effective user ID is neither the user ID of the file
415 to be replaced nor that of the directory containing it,
416 and the process is not privileged
417 (Linux: does not have the
418 .B CAP_FOWNER
419 capability);
420 or the filesystem containing
421 .I oldpath
422 does not support renaming of the type requested.
424 .B EROFS
425 The file is on a read-only filesystem.
427 .B EXDEV
428 .IR oldpath " and " newpath
429 are not on the same mounted filesystem.
430 (Linux permits a filesystem to be mounted at multiple points, but
431 .BR rename ()
432 does not work across different mount points,
433 even if the same filesystem is mounted on both.)
435 The following additional errors can occur for
436 .BR renameat ()
438 .BR renameat2 ():
440 .B EBADF
441 .I olddirfd
443 .I newdirfd
444 is not a valid file descriptor.
446 .B ENOTDIR
447 .I oldpath
448 is relative and
449 .I olddirfd
450 is a file descriptor referring to a file other than a directory;
451 or similar for
452 .I newpath
454 .I newdirfd
456 The following additional errors can occur for
457 .BR renameat2 ():
459 .B EEXIST
460 .I flags
461 contains
462 .B RENAME_NOREPLACE
464 .I newpath
465 already exists.
467 .B EINVAL
468 An invalid flag was specified in
469 .IR flags .
471 .B EINVAL
472 Both
473 .B RENAME_NOREPLACE
475 .B RENAME_EXCHANGE
476 were specified in
477 .IR flags .
479 .B EINVAL
480 Both
481 .B RENAME_WHITEOUT
483 .B RENAME_EXCHANGE
484 were specified in
485 .IR flags .
487 .B EINVAL
488 The filesystem does not support one of the flags in
489 .IR flags .
491 .B ENOENT
492 .I flags
493 contains
494 .B RENAME_EXCHANGE
496 .IR newpath
497 does not exist.
499 .B EPERM
500 .B RENAME_WHITEOUT
501 was specified in
502 .IR flags ,
503 but the caller does not have the
504 .B CAP_MKNOD
505 capability.
506 .SH VERSIONS
507 .BR renameat ()
508 was added to Linux in kernel 2.6.16;
509 library support was added to glibc in version 2.4.
511 .BR renameat2 ()
512 was added to Linux in kernel 3.15; library support was added in glibc 2.28.
513 .SH CONFORMING TO
514 .BR rename ():
515 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
517 .BR renameat ():
518 POSIX.1-2008.
520 .BR renameat2 ()
521 is Linux-specific.
522 .SH NOTES
524 .SS Glibc notes
525 On older kernels where
526 .BR renameat ()
527 is unavailable, the glibc wrapper function falls back to the use of
528 .BR rename ().
529 When
530 .I oldpath
532 .I newpath
533 are relative pathnames,
534 glibc constructs pathnames based on the symbolic links in
535 .IR /proc/self/fd
536 that correspond to the
537 .I olddirfd
539 .IR newdirfd
540 arguments.
541 .SH BUGS
542 On NFS filesystems, you can not assume that if the operation
543 failed, the file was not renamed.
544 If the server does the rename operation
545 and then crashes, the retransmitted RPC which will be processed when the
546 server is up again causes a failure.
547 The application is expected to
548 deal with this.
550 .BR link (2)
551 for a similar problem.
552 .SH SEE ALSO
553 .BR mv (1),
554 .BR rename (1),
555 .BR chmod (2),
556 .BR link (2),
557 .BR symlink (2),
558 .BR unlink (2),
559 .BR path_resolution (7),
560 .BR symlink (7)