#include <stdio.h>

       int rename(const char *oldpath, const char *newpath);

       #include <fcntl.h>           /* Definition of AT_* constants */
       #include <stdio.h>

       int renameat(int olddirfd, const char *oldpath,
                    int newdirfd, const char *newpath);

       int renameat2(int olddirfd, const char *oldpath,
                     int newdirfd, const char *newpath, unsigned int flags);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

           Since glibc 2.10:
               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:

       rename()  renames  a  file,  moving it between directories if required.
       Any other hard links to the file (as created using link(2))  are  unaf-
       fected.  Open file descriptors for oldpath are also unaffected.

       If newpath already exists, it will be atomically replaced (subject to a
       few conditions; see ERRORS below), so that there is no point  at  which
       another process attempting to access newpath will find it missing.

       If  oldpath  and  newpath are existing hard links referring to the same
       file, then rename() does nothing, and returns a success status.

       If newpath exists but the operation fails  for  some  reason,  rename()
       guarantees to leave an instance of newpath in place.

       oldpath can specify a directory.  In this case, newpath must either not
       exist, or it must specify an empty directory.

       However, when overwriting there will probably be a window in which both
       oldpath and newpath refer to the file being renamed.

       If  oldpath  refers to a symbolic link, the link is renamed; if newpath
       refers to a symbolic link, the link will be overwritten.

       The renameat()  system  call  operates  in  exactly  the  same  way  as
       rename(), except for the differences described here.

       If  the  pathname  given in oldpath is relative, then it is interpreted
       relative to the directory referred to by the file  descriptor  olddirfd
       (rather  than  relative to the current working directory of the calling
       process, as is done by rename() for a relative pathname).

       renameat2() has an additional flags argument.  A renameat2() call  with
       a zero flags argument is equivalent to renameat().

       The flags argument is a bit mask consisting of zero or more of the fol-
       lowing flags:

              Atomically exchange oldpath and newpath.   Both  pathnames  must
              exist  but  may be of different types (e.g., one could be a non-
              empty directory and the other a symbolic link).

              Don't overwrite newpath of the rename.  Return an error if  new-
              path already exists.

              RENAME_NOREPLACE     can't    be    employed    together    with

       RENAME_WHITEOUT (since Linux 3.18)
              This operation makes sense  only  for  overlay/union  filesystem

              Specifying  RENAME_WHITEOUT  creates  a "whiteout" object at the
              source of the rename at the same time as performing the  rename.
              The  whole  operation  is atomic, so that if the rename succeeds
              then the whiteout will also have been created.

              A  "whiteout"  is  an  object  that  has  special   meaning   in
              union/overlay  filesystem constructs.  In these constructs, mul-
              tiple layers exist and only the top one  is  ever  modified.   A
              whiteout on an upper layer will effectively hide a matching file
              in the lower layer, making it  appear  as  if  the  file  didn't

              When  a file that exists on the lower layer is renamed, the file
              is first copied up (if not already on the upper layer) and  then
              renamed  on  the upper, read-write layer.  At the same time, the
              source file needs to be "whiteouted" (so that the version of the
              source  file  in  the  lower  layer is rendered invisible).  The
              whole operation needs to be done atomically.

              When not part of a union/overlay,  the  whiteout  appears  as  a
              character device with a {0,0} device number.

              RENAME_WHITEOUT  requires  the  same  privileges  as  creating a
              device node (i.e., the CAP_MKNOD capability).

              RENAME_WHITEOUT can't be employed together with RENAME_EXCHANGE.

              RENAME_WHITEOUT requires support from the underlying filesystem.
              Among the filesystems that provide that support are shmem (since
              Linux 3.18), ext4 (since Linux 3.18), and XFS (since Linux 4.1).

              is in use by some process (perhaps as current working directory,
              or  as root directory, or because it was open for reading) or is
              in use by the system (for example as  mount  point),  while  the
              system considers this an error.  (Note that there is no require-
              ment to return EBUSY in such cases--there is nothing wrong  with
              doing  the  rename  anyway--but it is allowed to return EBUSY if
              the system cannot otherwise handle such situations.)

       EDQUOT The user's quota of disk  blocks  on  the  filesystem  has  been

       EFAULT oldpath or newpath points outside your accessible address space.

       EINVAL The  new  pathname  contained a path prefix of the old, or, more
              generally, an attempt was made to make a directory  a  subdirec-
              tory of itself.

       EISDIR newpath  is  an  existing directory, but oldpath is not a direc-

       ELOOP  Too many symbolic links were encountered in resolving oldpath or

       EMLINK oldpath already has the maximum number of links to it, or it was
              a directory and the directory containing newpath has the maximum
              number of links.

              oldpath or newpath was too long.

       ENOENT The link named by oldpath does not exist; or, a directory compo-
              nent in newpath does not exist; or, oldpath  or  newpath  is  an
              empty string.

       ENOMEM Insufficient kernel memory was available.

       ENOSPC The device containing the file has no room for the new directory

              A component used as a directory in oldpath or newpath is not, in
              fact,  a  directory.   Or,  oldpath  is a directory, and newpath
              exists but is not a directory.

              newpath is a nonempty directory, that is, contains entries other
              than "." and "..".

       EPERM or EACCES
              The  directory  containing  oldpath has the sticky bit (S_ISVTX)
              set and the process's effective user ID is neither the  user  ID
              of  the  file to be deleted nor that of the directory containing
              it, and the process is not privileged (Linux: does not have  the
              CAP_FOWNER  capability);  or newpath is an existing file and the
              if the same filesystem is mounted on both.)

       The  following  additional  errors  can  occur   for   renameat()   and

       EBADF  olddirfd or newdirfd is not a valid file descriptor.

              oldpath  is relative and olddirfd is a file descriptor referring
              to a file other than a directory; or  similar  for  newpath  and

       The following additional errors can occur for renameat2():

       EEXIST flags contains RENAME_NOREPLACE and newpath already exists.

       EINVAL An invalid flag was specified in flags.

       EINVAL Both  RENAME_NOREPLACE  and  RENAME_EXCHANGE  were  specified in

       EINVAL Both  RENAME_WHITEOUT  and  RENAME_EXCHANGE  were  specified  in

       EINVAL The filesystem does not support one of the flags in flags.

       ENOENT flags contains RENAME_EXCHANGE and newpath does not exist.

       EPERM  RENAME_WHITEOUT  was specified in flags, but the caller does not
              have the CAP_MKNOD capability.

       renameat() was added to Linux in kernel  2.6.16;  library  support  was
       added to glibc in version 2.4.

       renameat2() was added to Linux in kernel 3.15.

       rename(): 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.

       renameat(): POSIX.1-2008.

       renameat2() is Linux-specific.

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

       On  NFS  filesystems,  you can not assume that if the operation failed,
       latest    version    of    this    page,    can     be     found     at

Linux                             2015-07-23                         RENAME(2)
Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2019 Hurricane Electric. All Rights Reserved.