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