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(2).)
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 A directory component in oldpath or newpath does not exist or
77 is a dangling symbolic link.
78
79 ENOMEM Insufficient kernel memory was available.
80
81 ENOSPC The device containing the file has no room for the new directory
82 entry.
83
84 ENOTDIR
85 A component used as a directory in oldpath or newpath is not, in
86 fact, a directory. Or, oldpath is a directory, and newpath
87 exists but is not a directory.
88
89 ENOTEMPTY or EEXIST
90 newpath is a non-empty directory, i.e., contains entries other
91 than "." and "..".
92
93 EPERM or EACCES
94 The directory containing oldpath has the sticky bit (S_ISVTX)
95 set and the process's effective user ID is neither the user ID
96 of the file to be deleted nor that of the directory containing
97 it, and the process is not privileged (Linux: does not have the
98 CAP_FOWNER capability); or newpath is an existing file and the
99 directory containing it has the sticky bit set and the process's
100 effective user ID is neither the user ID of the file to be
101 replaced nor that of the directory containing it, and the
102 process is not privileged (Linux: does not have the CAP_FOWNER
103 capability); or the filesystem containing pathname does not sup‐
104 port renaming of the type requested.
105
106 EROFS The file is on a read-only filesystem.
107
108 EXDEV oldpath and newpath are not on the same mounted filesystem.
109 (Linux permits a filesystem to be mounted at multiple points,
110 but rename(2) does not work across different mount points, even
111 if the same filesystem is mounted on both.)
112
114 4.3BSD, C89, C99, POSIX.1-2001.
115
117 On NFS filesystems, you can not assume that if the operation failed the
118 file was not renamed. If the server does the rename operation and then
119 crashes, the retransmitted RPC which will be processed when the server
120 is up again causes a failure. The application is expected to deal with
121 this. See link(2) for a similar problem.
122
124 mv(1), chmod(2), link(2), path_resolution(2), renameat(2), symlink(2),
125 unlink(2)
126
127
128
129Linux 2.0 1998-06-04 RENAME(2)