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

NAME

6       rename - 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

DESCRIPTION

14       rename()  renames  a  file,  moving it between directories if required.
15       Any other hard links to the file (as created using link(2))  are  unaf‐
16       fected.  Open file descriptors for oldpath are also unaffected.
17
18       If  newpath already exists it will be atomically replaced (subject to a
19       few conditions; see ERRORS below), so that there is no point  at  which
20       another process attempting to access newpath will find it missing.
21
22       If  oldpath  and  newpath are existing hard links referring to the same
23       file, then rename() does nothing, and returns a success status.
24
25       If newpath exists but the operation  fails  for  some  reason  rename()
26       guarantees to leave an instance of newpath in place.
27
28       oldpath can specify a directory.  In this case, newpath must either not
29       exist, or it must specify an empty directory.
30
31       However, when overwriting there will probably be a window in which both
32       oldpath and newpath refer to the file being renamed.
33
34       If  oldpath  refers  to a symbolic link the link is renamed; if newpath
35       refers to a symbolic link the link will be overwritten.
36

RETURN VALUE

38       On success, zero is returned.  On error, -1 is returned, and  errno  is
39       set appropriately.
40

ERRORS

42       EACCES Write  permission is denied for the directory containing oldpath
43              or newpath, or, search permission  is  denied  for  one  of  the
44              directories in the path prefix of oldpath or newpath, or oldpath
45              is a directory and does not allow write  permission  (needed  to
46              update the ..  entry).  (See also path_resolution(7).)
47
48       EBUSY  The  rename fails because oldpath or newpath is a directory that
49              is in use by some process (perhaps as current working directory,
50              or  as root directory, or because it was open for reading) or is
51              in use by the system (for example as  mount  point),  while  the
52              system considers this an error.  (Note that there is no require‐
53              ment to return EBUSY in such cases—there is nothing  wrong  with
54              doing the rename anyway—but it is allowed to return EBUSY if the
55              system cannot otherwise handle such situations.)
56
57       EDQUOT The user's quota of disk blocks on  the  file  system  has  been
58              exhausted.
59
60       EFAULT oldpath or newpath points outside your accessible address space.
61
62       EINVAL The  new  pathname  contained a path prefix of the old, or, more
63              generally, an attempt was made to make a directory  a  subdirec‐
64              tory of itself.
65
66       EISDIR newpath  is  an  existing directory, but oldpath is not a direc‐
67              tory.
68
69       ELOOP  Too many symbolic links were encountered in resolving oldpath or
70              newpath.
71
72       EMLINK oldpath already has the maximum number of links to it, or it was
73              a directory and the directory containing newpath has the maximum
74              number of links.
75
76       ENAMETOOLONG
77              oldpath or newpath was too long.
78
79       ENOENT The link named by oldpath does not exist; or, a directory compo‐
80              nent in newpath does not exist; or, oldpath  or  newpath  is  an
81              empty string.
82
83       ENOMEM Insufficient kernel memory was available.
84
85       ENOSPC The device containing the file has no room for the new directory
86              entry.
87
88       ENOTDIR
89              A component used as a directory in oldpath or newpath is not, in
90              fact,  a  directory.   Or,  oldpath  is a directory, and newpath
91              exists but is not a directory.
92
93       ENOTEMPTY or EEXIST
94              newpath is a nonempty directory, that is, contains entries other
95              than "." and "..".
96
97       EPERM or EACCES
98              The  directory  containing  oldpath has the sticky bit (S_ISVTX)
99              set and the process's effective user ID is neither the  user  ID
100              of  the  file to be deleted nor that of the directory containing
101              it, and the process is not privileged (Linux: does not have  the
102              CAP_FOWNER  capability);  or newpath is an existing file and the
103              directory containing it has the sticky bit set and the process's
104              effective  user  ID  is  neither  the  user ID of the file to be
105              replaced nor that  of  the  directory  containing  it,  and  the
106              process  is  not privileged (Linux: does not have the CAP_FOWNER
107              capability); or the file system  containing  pathname  does  not
108              support renaming of the type requested.
109
110       EROFS  The file is on a read-only file system.
111
112       EXDEV  oldpath  and  newpath  are  not on the same mounted file system.
113              (Linux permits a file system to be mounted at  multiple  points,
114              but  rename()  does not work across different mount points, even
115              if the same file system is mounted on both.)
116

CONFORMING TO

118       4.3BSD, C89, C99, POSIX.1-2001.
119

BUGS

121       On NFS file systems, you can not assume that if  the  operation  failed
122       the  file was not renamed.  If the server does the rename operation and
123       then crashes, the retransmitted RPC which will be  processed  when  the
124       server  is  up  again causes a failure.  The application is expected to
125       deal with this.  See link(2) for a similar problem.
126

SEE ALSO

128       mv(1), chmod(2), link(2), renameat(2), symlink(2), unlink(2), path_res‐
129       olution(7), symlink(7)
130

COLOPHON

132       This  page  is  part of release 3.53 of the Linux man-pages project.  A
133       description of the project, and information about reporting  bugs,  can
134       be found at http://www.kernel.org/doc/man-pages/.
135
136
137
138Linux                             2013-01-27                         RENAME(2)
Impressum