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       Note: There is no glibc wrapper for renameat2(); see NOTES.
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

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_WHITEOUT (since Linux 3.18)
98              This  operation  makes  sense  only for overlay/union filesystem
99              implementations.
100
101              Specifying RENAME_WHITEOUT creates a "whiteout"  object  at  the
102              source  of the rename at the same time as performing the rename.
103              The whole operation is atomic, so that if  the  rename  succeeds
104              then the whiteout will also have been created.
105
106              A   "whiteout"   is  an  object  that  has  special  meaning  in
107              union/overlay filesystem constructs.  In these constructs,  mul‐
108              tiple  layers  exist  and  only the top one is ever modified.  A
109              whiteout on an upper layer will effectively hide a matching file
110              in  the  lower  layer,  making  it  appear as if the file didn't
111              exist.
112
113              When a file that exists on the lower layer is renamed, the  file
114              is  first copied up (if not already on the upper layer) and then
115              renamed on the upper, read-write layer.  At the same  time,  the
116              source file needs to be "whiteouted" (so that the version of the
117              source file in the lower  layer  is  rendered  invisible).   The
118              whole operation needs to be done atomically.
119
120              When  not  part  of  a  union/overlay, the whiteout appears as a
121              character device with a {0,0} device number.
122
123              RENAME_WHITEOUT requires  the  same  privileges  as  creating  a
124              device node (i.e., the CAP_MKNOD capability).
125
126              RENAME_WHITEOUT can't be employed together with RENAME_EXCHANGE.
127
128              RENAME_WHITEOUT requires support from the underlying filesystem.
129              Among the filesystems that provide that support are shmem (since
130              Linux 3.18), ext4 (since Linux 3.18), and XFS (since Linux 4.1).
131

RETURN VALUE

133       On  success,  zero is returned.  On error, -1 is returned, and errno is
134       set appropriately.
135

ERRORS

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

VERSIONS

242       renameat()  was  added  to  Linux in kernel 2.6.16; library support was
243       added to glibc in version 2.4.
244
245       renameat2() was added to Linux in kernel 3.15.
246

CONFORMING TO

248       rename(): 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
249
250       renameat(): POSIX.1-2008.
251
252       renameat2() is Linux-specific.
253

NOTES

255       Glibc does not provide a wrapper for the renameat2() system call;  call
256       it using syscall(2).
257
258   Glibc notes
259       On  older  kernels  where  renameat() is unavailable, the glibc wrapper
260       function falls back to the use of rename().  When oldpath  and  newpath
261       are  relative  pathnames,  glibc constructs pathnames based on the sym‐
262       bolic links in  /proc/self/fd  that  correspond  to  the  olddirfd  and
263       newdirfd arguments.
264

BUGS

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

SEE ALSO

273       mv(1),  chmod(2),  link(2),  symlink(2), unlink(2), path_resolution(7),
274       symlink(7)
275

COLOPHON

277       This page is part of release 4.16 of the Linux  man-pages  project.   A
278       description  of  the project, information about reporting bugs, and the
279       latest    version    of    this    page,    can     be     found     at
280       https://www.kernel.org/doc/man-pages/.
281
282
283
284Linux                             2017-09-15                         RENAME(2)
Impressum