1rename(2)                     System Calls Manual                    rename(2)
2
3
4

NAME

6       rename, renameat, renameat2 - change the name or location of a file
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdio.h>
13
14       int rename(const char *oldpath, const char *newpath);
15
16       #include <fcntl.h>           /* Definition of AT_* constants */
17       #include <stdio.h>
18
19       int renameat(int olddirfd, const char *oldpath,
20                    int newdirfd, const char *newpath);
21       int renameat2(int olddirfd, const char *oldpath,
22                    int newdirfd, const char *newpath, unsigned int flags);
23
24   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
25
26       renameat():
27           Since glibc 2.10:
28               _POSIX_C_SOURCE >= 200809L
29           Before glibc 2.10:
30               _ATFILE_SOURCE
31
32       renameat2():
33           _GNU_SOURCE
34

DESCRIPTION

36       rename()  renames  a  file,  moving it between directories if required.
37       Any other hard links to the file (as created using link(2))  are  unaf‐
38       fected.  Open file descriptors for oldpath are also unaffected.
39
40       Various restrictions determine whether or not the rename operation suc‐
41       ceeds: see ERRORS below.
42
43       If newpath already exists, it will  be  atomically  replaced,  so  that
44       there is no point at which another process attempting to access newpath
45       will find it missing.  However, there will  probably  be  a  window  in
46       which both oldpath and newpath refer to the file being renamed.
47
48       If  oldpath  and  newpath are existing hard links referring to the same
49       file, then rename() does nothing, and returns a success status.
50
51       If newpath exists but the operation fails  for  some  reason,  rename()
52       guarantees to leave an instance of newpath in place.
53
54       oldpath can specify a directory.  In this case, newpath must either not
55       exist, or it must specify an empty directory.
56
57       If oldpath refers to a symbolic link, the link is renamed;  if  newpath
58       refers to a symbolic link, the link will be overwritten.
59
60   renameat()
61       The  renameat()  system  call  operates  in exactly the same way as re‐
62       name(), except for the differences described here.
63
64       If the pathname given in oldpath is relative, then  it  is  interpreted
65       relative  to  the directory referred to by the file descriptor olddirfd
66       (rather than relative to the current working directory of  the  calling
67       process, as is done by rename() for a relative pathname).
68
69       If oldpath is relative and olddirfd is the special value AT_FDCWD, then
70       oldpath is interpreted relative to the current working directory of the
71       calling process (like rename()).
72
73       If oldpath is absolute, then olddirfd is ignored.
74
75       The interpretation of newpath is as for oldpath, except that a relative
76       pathname is interpreted relative to the directory referred  to  by  the
77       file descriptor newdirfd.
78
79       See openat(2) for an explanation of the need for renameat().
80
81   renameat2()
82       renameat2()  has an additional flags argument.  A renameat2() call with
83       a zero flags argument is equivalent to renameat().
84
85       The flags argument is a bit mask consisting of zero or more of the fol‐
86       lowing flags:
87
88       RENAME_EXCHANGE
89              Atomically  exchange  oldpath  and newpath.  Both pathnames must
90              exist but may be of different types (e.g., one could be  a  non-
91              empty directory and the other a symbolic link).
92
93       RENAME_NOREPLACE
94              Don't  overwrite newpath of the rename.  Return an error if new‐
95              path already exists.
96
97              RENAME_NOREPLACE can't  be  employed  together  with  RENAME_EX‐
98              CHANGE.
99
100              RENAME_NOREPLACE  requires  support from the underlying filesys‐
101              tem.  Support for various filesystems was added as follows:
102
103              •  ext4 (Linux 3.15);
104
105              •  btrfs, tmpfs, and cifs (Linux 3.17);
106
107              •  xfs (Linux 4.0);
108
109              •  Support for many other filesystems was added  in  Linux  4.9,
110                 including ext2, minix, reiserfs, jfs, vfat, and bpf.
111
112       RENAME_WHITEOUT (since Linux 3.18)
113              This operation makes sense only for overlay/union filesystem im‐
114              plementations.
115
116              Specifying RENAME_WHITEOUT creates a "whiteout"  object  at  the
117              source  of the rename at the same time as performing the rename.
118              The whole operation is atomic, so that if  the  rename  succeeds
119              then the whiteout will also have been created.
120
121              A   "whiteout"   is  an  object  that  has  special  meaning  in
122              union/overlay filesystem constructs.  In these constructs,  mul‐
123              tiple  layers  exist  and  only the top one is ever modified.  A
124              whiteout on an upper layer will effectively hide a matching file
125              in  the  lower layer, making it appear as if the file didn't ex‐
126              ist.
127
128              When a file that exists on the lower layer is renamed, the  file
129              is  first copied up (if not already on the upper layer) and then
130              renamed on the upper, read-write layer.  At the same  time,  the
131              source file needs to be "whiteouted" (so that the version of the
132              source file in the lower  layer  is  rendered  invisible).   The
133              whole operation needs to be done atomically.
134
135              When  not  part  of  a  union/overlay, the whiteout appears as a
136              character device with a {0,0} device number.  (Note  that  other
137              union/overlay  implementations  may employ different methods for
138              storing whiteout entries; specifically, BSD union mount  employs
139              a  separate  inode  type, DT_WHT, which, while supported by some
140              filesystems available in Linux, such as CODA and XFS, is ignored
141              by  the  kernel's  whiteout  support  code, as of Linux 4.19, at
142              least.)
143
144              RENAME_WHITEOUT requires the same privileges as creating  a  de‐
145              vice node (i.e., the CAP_MKNOD capability).
146
147              RENAME_WHITEOUT can't be employed together with RENAME_EXCHANGE.
148
149              RENAME_WHITEOUT requires support from the underlying filesystem.
150              Among the filesystems that support it  are  tmpfs  (since  Linux
151              3.18),  ext4  (since  Linux  3.18),  XFS (since Linux 4.1), f2fs
152              (since Linux 4.2), btrfs (since Linux  4.7),  and  ubifs  (since
153              Linux 4.9).
154

RETURN VALUE

156       On  success,  zero is returned.  On error, -1 is returned, and errno is
157       set to indicate the error.
158

ERRORS

160       EACCES Write permission is denied for the directory containing  oldpath
161              or  newpath,  or, search permission is denied for one of the di‐
162              rectories in the path prefix of oldpath or newpath,  or  oldpath
163              is  a  directory  and does not allow write permission (needed to
164              update the ..  entry).  (See also path_resolution(7).)
165
166       EBUSY  The rename fails because oldpath or newpath is a directory  that
167              is in use by some process (perhaps as current working directory,
168              or as root directory, or because it was open for reading) or  is
169              in  use  by the system (for example as a mount point), while the
170              system considers this an error.  (Note that there is no require‐
171              ment  to  return EBUSY in such cases—there is nothing wrong with
172              doing the rename anyway—but it is allowed to return EBUSY if the
173              system cannot otherwise handle such situations.)
174
175       EDQUOT The  user's  quota of disk blocks on the filesystem has been ex‐
176              hausted.
177
178       EFAULT oldpath or newpath points outside your accessible address space.
179
180       EINVAL The new pathname contained a path prefix of the  old,  or,  more
181              generally,  an  attempt was made to make a directory a subdirec‐
182              tory of itself.
183
184       EISDIR newpath is an existing directory, but oldpath is  not  a  direc‐
185              tory.
186
187       ELOOP  Too many symbolic links were encountered in resolving oldpath or
188              newpath.
189
190       EMLINK oldpath already has the maximum number of links to it, or it was
191              a directory and the directory containing newpath has the maximum
192              number of links.
193
194       ENAMETOOLONG
195              oldpath or newpath was too long.
196
197       ENOENT The link named by oldpath does not exist; or, a directory compo‐
198              nent  in  newpath  does  not exist; or, oldpath or newpath is an
199              empty string.
200
201       ENOMEM Insufficient kernel memory was available.
202
203       ENOSPC The device containing the file has no room for the new directory
204              entry.
205
206       ENOTDIR
207              A component used as a directory in oldpath or newpath is not, in
208              fact, a directory.  Or, oldpath is a directory, and newpath  ex‐
209              ists but is not a directory.
210
211       ENOTEMPTY or EEXIST
212              newpath is a nonempty directory, that is, contains entries other
213              than "." and "..".
214
215       EPERM or EACCES
216              The directory containing oldpath has the  sticky  bit  (S_ISVTX)
217              set  and  the process's effective user ID is neither the user ID
218              of the file to be deleted nor that of the  directory  containing
219              it,  and the process is not privileged (Linux: does not have the
220              CAP_FOWNER capability); or newpath is an existing file  and  the
221              directory containing it has the sticky bit set and the process's
222              effective user ID is neither the user ID of the file to  be  re‐
223              placed  nor that of the directory containing it, and the process
224              is not privileged (Linux: does not have the CAP_FOWNER  capabil‐
225              ity);  or the filesystem containing oldpath does not support re‐
226              naming of the type requested.
227
228       EROFS  The file is on a read-only filesystem.
229
230       EXDEV  oldpath and newpath are not  on  the  same  mounted  filesystem.
231              (Linux  permits  a  filesystem to be mounted at multiple points,
232              but rename() does not work across different mount  points,  even
233              if the same filesystem is mounted on both.)
234
235       The  following  additional  errors  can  occur  for  renameat() and re‐
236       nameat2():
237
238       EBADF  oldpath (newpath) is relative but olddirfd (newdirfd) is  not  a
239              valid file descriptor.
240
241       ENOTDIR
242              oldpath  is relative and olddirfd is a file descriptor referring
243              to a file other than a directory; or  similar  for  newpath  and
244              newdirfd
245
246       The following additional errors can occur for renameat2():
247
248       EEXIST flags contains RENAME_NOREPLACE and newpath already exists.
249
250       EINVAL An invalid flag was specified in flags.
251
252       EINVAL Both  RENAME_NOREPLACE  and  RENAME_EXCHANGE  were  specified in
253              flags.
254
255       EINVAL Both  RENAME_WHITEOUT  and  RENAME_EXCHANGE  were  specified  in
256              flags.
257
258       EINVAL The filesystem does not support one of the flags in flags.
259
260       ENOENT flags contains RENAME_EXCHANGE and newpath does not exist.
261
262       EPERM  RENAME_WHITEOUT  was specified in flags, but the caller does not
263              have the CAP_MKNOD capability.
264

STANDARDS

266       rename()
267              C11, POSIX.1-2008.
268
269       renameat()
270              POSIX.1-2008.
271
272       renameat2()
273              Linux.
274

HISTORY

276       rename()
277              4.3BSD, C89, POSIX.1-2001.
278
279       renameat()
280              Linux 2.6.16, glibc 2.4.
281
282       renameat2()
283              Linux 3.15, glibc 2.28.
284
285   glibc notes
286       On older kernels where renameat() is  unavailable,  the  glibc  wrapper
287       function  falls  back to the use of rename().  When oldpath and newpath
288       are relative pathnames, glibc constructs pathnames based  on  the  sym‐
289       bolic  links  in  /proc/self/fd  that  correspond  to  the olddirfd and
290       newdirfd arguments.
291

BUGS

293       On NFS filesystems, you can not assume that if  the  operation  failed,
294       the  file was not renamed.  If the server does the rename operation and
295       then crashes, the retransmitted RPC which will be  processed  when  the
296       server  is  up  again causes a failure.  The application is expected to
297       deal with this.  See link(2) for a similar problem.
298

SEE ALSO

300       mv(1), rename(1), chmod(2), link(2), symlink(2), unlink(2),  path_reso‐
301       lution(7), symlink(7)
302
303
304
305Linux man-pages 6.04              2023-03-30                         rename(2)
Impressum