1RENAME(2)                  Linux Programmer's Manual                 RENAME(2)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

153       On  success,  zero is returned.  On error, -1 is returned, and errno is
154       set to indicate the error.
155

ERRORS

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

VERSIONS

263       renameat() was added to Linux in kernel  2.6.16;  library  support  was
264       added to glibc in version 2.4.
265
266       renameat2()  was  added  to  Linux  in kernel 3.15; library support was
267       added in glibc 2.28.
268

CONFORMING TO

270       rename(): 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
271
272       renameat(): POSIX.1-2008.
273
274       renameat2() is Linux-specific.
275

NOTES

277   Glibc notes
278       On older kernels where renameat() is  unavailable,  the  glibc  wrapper
279       function  falls  back to the use of rename().  When oldpath and newpath
280       are relative pathnames, glibc constructs pathnames based  on  the  sym‐
281       bolic  links  in  /proc/self/fd  that  correspond  to  the olddirfd and
282       newdirfd arguments.
283

BUGS

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

SEE ALSO

292       mv(1), rename(1), chmod(2), link(2), symlink(2), unlink(2),  path_reso‐
293       lution(7), symlink(7)
294

COLOPHON

296       This  page  is  part of release 5.13 of the Linux man-pages project.  A
297       description of the project, information about reporting bugs,  and  the
298       latest     version     of     this    page,    can    be    found    at
299       https://www.kernel.org/doc/man-pages/.
300
301
302
303Linux                             2021-08-27                         RENAME(2)
Impressum