1RENAME(2) Linux Programmer's Manual RENAME(2)
2
3
4
6 rename - change the name or location of a file
7
9 #include <stdio.h>
10
11 int rename(const char *oldpath, const char *newpath);
12
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
38 On success, zero is returned. On error, -1 is returned, and errno is
39 set appropriately.
40
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
55 the system cannot otherwise handle such situations.)
56
57 EFAULT oldpath or newpath points outside your accessible address space.
58
59 EINVAL The new pathname contained a path prefix of the old, or, more
60 generally, an attempt was made to make a directory a subdirec‐
61 tory of itself.
62
63 EISDIR newpath is an existing directory, but oldpath is not a direc‐
64 tory.
65
66 ELOOP Too many symbolic links were encountered in resolving oldpath or
67 newpath.
68
69 EMLINK oldpath already has the maximum number of links to it, or it was
70 a directory and the directory containing newpath has the maximum
71 number of links.
72
73 ENAMETOOLONG
74 oldpath or newpath was too long.
75
76 ENOENT The link named by oldpath does not exist; or, a directory compo‐
77 nent in newpath does not exist; or, oldpath or newpath is an
78 empty string.
79
80 ENOMEM Insufficient kernel memory was available.
81
82 ENOSPC The device containing the file has no room for the new directory
83 entry.
84
85 ENOTDIR
86 A component used as a directory in oldpath or newpath is not, in
87 fact, a directory. Or, oldpath is a directory, and newpath
88 exists but is not a directory.
89
90 ENOTEMPTY or EEXIST
91 newpath is a nonempty directory, that is, contains entries other
92 than "." and "..".
93
94 EPERM or EACCES
95 The directory containing oldpath has the sticky bit (S_ISVTX)
96 set and the process's effective user ID is neither the user ID
97 of the file to be deleted nor that of the directory containing
98 it, and the process is not privileged (Linux: does not have the
99 CAP_FOWNER capability); or newpath is an existing file and the
100 directory containing it has the sticky bit set and the process's
101 effective user ID is neither the user ID of the file to be
102 replaced nor that of the directory containing it, and the
103 process is not privileged (Linux: does not have the CAP_FOWNER
104 capability); or the file system containing pathname does not
105 support renaming of the type requested.
106
107 EROFS The file is on a read-only file system.
108
109 EXDEV oldpath and newpath are not on the same mounted file system.
110 (Linux permits a file system to be mounted at multiple points,
111 but rename() does not work across different mount points, even
112 if the same file system is mounted on both.)
113
115 4.3BSD, C89, C99, POSIX.1-2001.
116
118 On NFS file systems, you can not assume that if the operation failed
119 the file was not renamed. If the server does the rename operation and
120 then crashes, the retransmitted RPC which will be processed when the
121 server is up again causes a failure. The application is expected to
122 deal with this. See link(2) for a similar problem.
123
125 mv(1), chmod(2), link(2), renameat(2), symlink(2), unlink(2), path_res‐
126 olution(7), symlink(7)
127
129 This page is part of release 3.25 of the Linux man-pages project. A
130 description of the project, and information about reporting bugs, can
131 be found at http://www.kernel.org/doc/man-pages/.
132
133
134
135Linux 2009-03-30 RENAME(2)