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
19       int renameat2(int olddirfd, const char *oldpath,
20                     int newdirfd, const char *newpath, unsigned int flags);
21
22   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24       renameat():
25           Since glibc 2.10:
26               _POSIX_C_SOURCE >= 200809L
27           Before glibc 2.10:
28               _ATFILE_SOURCE
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
59       rename(), 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
95              RENAME_EXCHANGE.
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, shmem, 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 etx2, minix, reiserfs, jfs, vfat, and bpf.
108
109       RENAME_WHITEOUT (since Linux 3.18)
110              This  operation  makes  sense  only for overlay/union filesystem
111              implementations.
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
123              exist.
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
142              device 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 provide that support are tmpfs (since
148              Linux  3.18),  ext4  (since  Linux 3.18), XFS (since Linux 4.1),
149              f2fs (since Linux 4.2).  btrfs  (since  Linux  4.7),  and  ubifs
150              (since Linux 4.9).
151

RETURN VALUE

153       On  success,  zero is returned.  On error, -1 is returned, and errno is
154       set appropriately.
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
159              directories 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 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
173              exhausted.
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
206              exists 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
220              replaced  nor  that  of  the  directory  containing  it, and the
221              process is not privileged (Linux: does not have  the  CAP_FOWNER
222              capability); or the filesystem containing pathname does not sup‐
223              port renaming 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
233       renameat2():
234
235       EBADF  olddirfd or newdirfd is not a valid file descriptor.
236
237       ENOTDIR
238              oldpath is relative and olddirfd is a file descriptor  referring
239              to  a  file  other  than a directory; or similar for newpath and
240              newdirfd
241
242       The following additional errors can occur for renameat2():
243
244       EEXIST flags contains RENAME_NOREPLACE and newpath already exists.
245
246       EINVAL An invalid flag was specified in flags.
247
248       EINVAL Both RENAME_NOREPLACE  and  RENAME_EXCHANGE  were  specified  in
249              flags.
250
251       EINVAL Both  RENAME_WHITEOUT  and  RENAME_EXCHANGE  were  specified  in
252              flags.
253
254       EINVAL The filesystem does not support one of the flags in flags.
255
256       ENOENT flags contains RENAME_EXCHANGE and newpath does not exist.
257
258       EPERM  RENAME_WHITEOUT was specified in flags, but the caller does  not
259              have the CAP_MKNOD capability.
260

VERSIONS

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

CONFORMING TO

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

NOTES

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

BUGS

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

SEE ALSO

291       mv(1),  chmod(2),  link(2),  symlink(2), unlink(2), path_resolution(7),
292       symlink(7)
293

COLOPHON

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