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