std::experimental::filesystem::rename (3) - Linux Manuals
std::experimental::filesystem::rename: std::experimental::filesystem::rename
Command to display std::experimental::filesystem::rename
manual in Linux: $ man 3 std::experimental::filesystem::rename
NAME
std::experimental::filesystem::rename - std::experimental::filesystem::rename
Synopsis
Defined in header <experimental/filesystem>
void rename(const path& old_p, const path& new_p); (filesystem TS)
void rename(const path& old_p, const path& new_p, std::error_code& ec);
Moves or renames the filesystem object identified by old_p to new_p as if by the POSIX rename:
* If old_p is a non-directory file, then new_p must be one of:
* the same file as old_p or a hardlink to it: nothing is done in this case
* existing non-directory file: new_p is first deleted, then, without allowing other processes to observe new_p as deleted, the pathname new_p is linked to the file and old_p is unlinked from the file. Write permissions are required to both the directory that contains old_p and the directory that contains new_p.
* non-existing file in an existing directory: The pathname new_p is linked to the file and old_p is unlinked from the file. Write permissions are required to both the directory that contains old_p and the directory that contains new_p.
* If old_p is a directory, then new_p must be one of:
* the same directory as old_p or a hardlink to it: nothing is done in this case
* existing directory: new_p is deleted if empty on POSIX systems, but this may be an error on other systems. If not an error, then new_p is first deleted, then, without allowing other processes to observe new_p as deleted, the pathname new_p is linked to the directory and old_p is unlinked from the directory. Write permissions are required to both the directory that contains old_p and the directory that contains new_p.
* non-existing directory, not ending with a directory separator, and whose parent directory exists: The pathname new_p is linked to the directory and old_p is unlinked from the directory. Write permissions are required to both the directory that contains old_p and the directory that contains new_p.
* Symlinks are not followed: if old_p is a symlink, it is itself renamed, not its target. If new_p is an existing symlink, it is itself erased, not its target.
Rename fails if
* new_p ends with dot or with dot-dot
* new_p names a non-existing directory ending with a directory separator
* old_p is a directory which is an ancestor of new_p
Parameters
old_p - path to move or rename
new_p - target path for the move/rename operation
ec - out-parameter for error reporting in the non-throwing overload
Return value
(none)
Exceptions
The overload that does not take a error_code& parameter throws filesystem_error on underlying OS API errors, constructed with old_p as the first argument, new_p as the second argument, and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking a error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has
noexcept specification:
noexcept
Example
// Run this code
#include <iostream>
#include <fstream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
fs::path p = fs::current_path() / "sandbox";
fs::create_directories(p/"from");
std::ofstream(p/"from/file1.txt").put('a');
fs::create_directory(p/"to");
// fs::rename(p/"from/file1.txt", p/"to/"); // error: to is a directory
fs::rename(p/"from/file1.txt", p/"to/file2.txt"); // OK
// fs::rename(p/"from", p/"to"); // error: to is not empty
fs::rename(p/"from", p/"to/subdir"); // OK
fs::remove_all(p);
}
See also
renames a file
rename (function)
removes a file or empty directory
remove removes a file or directory and all its contents, recursively
remove_all (function)
Pages related to std::experimental::filesystem::rename
- std::experimental::filesystem::read_symlink (3) - std::experimental::filesystem::read_symlink
- std::experimental::filesystem::recursive_directory_iterator (3) - std::experimental::filesystem::recursive_directory_iterator
- std::experimental::filesystem::recursive_directory_iterator::depth (3) - std::experimental::filesystem::recursive_directory_iterator::depth
- std::experimental::filesystem::recursive_directory_iterator::disable_recursion_pending (3) - std::experimental::filesystem::recursive_directory_iterator::disable_recursion_pending
- std::experimental::filesystem::recursive_directory_iterator::operator*, (3) - std::experimental::filesystem::recursive_directory_iterator::operator*,
- std::experimental::filesystem::recursive_directory_iterator::operator*,std::experimental::filesystem::recursive_directory_iterator::operator-> (3) - std::experimental::filesystem::recursive_directory_iterator::operator*,std::experimental::filesystem::recursive_directory_iterator::operator->
- std::experimental::filesystem::recursive_directory_iterator::operator++,increment (3) - std::experimental::filesystem::recursive_directory_iterator::operator++,increment
- std::experimental::filesystem::recursive_directory_iterator::operator= (3) - std::experimental::filesystem::recursive_directory_iterator::operator=
- std::experimental::filesystem::recursive_directory_iterator::options (3) - std::experimental::filesystem::recursive_directory_iterator::options
- std::experimental::filesystem::recursive_directory_iterator::pop (3) - std::experimental::filesystem::recursive_directory_iterator::pop
- std::experimental::filesystem::recursive_directory_iterator::recursion_pending (3) - std::experimental::filesystem::recursive_directory_iterator::recursion_pending
- std::experimental::filesystem::recursive_directory_iterator::recursive_directory_iterator (3) - std::experimental::filesystem::recursive_directory_iterator::recursive_directory_iterator
- std::experimental::filesystem::remove,std::experimental::filesystem::remove_all (3) - std::experimental::filesystem::remove,std::experimental::filesystem::remove_all
- std::experimental::filesystem::resize_file (3) - std::experimental::filesystem::resize_file
- std::experimental::filesystem::absolute,std::experimental::filesystem::system_complete (3) - std::experimental::filesystem::absolute,std::experimental::filesystem::system_complete
- std::experimental::filesystem::begin(directory_iterator), (3) - std::experimental::filesystem::begin(directory_iterator),
- std::experimental::filesystem::begin(directory_iterator),std::experimental::filesystem::end(directory_iterator) (3) - std::experimental::filesystem::begin(directory_iterator),std::experimental::filesystem::end(directory_iterator)
- std::experimental::filesystem::begin(recursive_directory_iterator), (3) - std::experimental::filesystem::begin(recursive_directory_iterator),
- std::experimental::filesystem::begin(recursive_directory_iterator),std::experimental::filesystem::end(recursive_directory_iterator) (3) - std::experimental::filesystem::begin(recursive_directory_iterator),std::experimental::filesystem::end(recursive_directory_iterator)
- std::experimental::filesystem::canonical (3) - std::experimental::filesystem::canonical
- std::experimental::filesystem::copy (3) - std::experimental::filesystem::copy
- std::experimental::filesystem::copy_file (3) - std::experimental::filesystem::copy_file
- std::experimental::filesystem::copy_options (3) - std::experimental::filesystem::copy_options
- std::experimental::filesystem::copy_symlink (3) - std::experimental::filesystem::copy_symlink
- std::experimental::filesystem::create_directory, (3) - std::experimental::filesystem::create_directory,
- std::experimental::filesystem::create_directory,std::experimental::filesystem::create_directories (3) - std::experimental::filesystem::create_directory,std::experimental::filesystem::create_directories
- std::experimental::filesystem::create_hard_link (3) - std::experimental::filesystem::create_hard_link
- std::experimental::filesystem::create_symlink, (3) - std::experimental::filesystem::create_symlink,
- std::experimental::filesystem::create_symlink,std::experimental::filesystem::create_directory_symlink (3) - std::experimental::filesystem::create_symlink,std::experimental::filesystem::create_directory_symlink
- std::experimental::filesystem::current_path (3) - std::experimental::filesystem::current_path
- std::experimental::filesystem::directory_entry (3) - std::experimental::filesystem::directory_entry
- std::experimental::filesystem::directory_entry::assign (3) - std::experimental::filesystem::directory_entry::assign
- std::experimental::filesystem::directory_entry::directory_entry (3) - std::experimental::filesystem::directory_entry::directory_entry
- std::experimental::filesystem::directory_entry::operator= (3) - std::experimental::filesystem::directory_entry::operator=
- std::experimental::filesystem::directory_entry::operator==,!=,<,<=,>,>= (3) - std::experimental::filesystem::directory_entry::operator==,!=,<,<=,>,>=