[fs.op.copy]

# 31 Input/output library [[input.output]](./#input.output)

## 31.12 File systems [[filesystems]](filesystems#fs.op.copy)

### 31.12.13 Filesystem operation functions [[fs.op.funcs]](fs.op.funcs#fs.op.copy)

#### 31.12.13.4 Copy [fs.op.copy]

[ð](#lib:copy,path)

`void filesystem::copy(const path& from, const path& to);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17253)

*Effects*: Equivalent tocopy(from, to, copy_options​::​none)[.](#1.sentence-1)

[ð](#lib:copy,path_)

`void filesystem::copy(const path& from, const path& to, error_code& ec);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17265)

*Effects*: Equivalent tocopy(from, to, copy_options​::​none, ec)[.](#2.sentence-1)

[ð](#lib:copy,path__)

`void filesystem::copy(const path& from, const path& to, copy_options options);
void filesystem::copy(const path& from, const path& to, copy_options options,
                      error_code& ec);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17279)

*Preconditions*: At most one element from each option group ([[fs.enum.copy.opts]](fs.enum.copy.opts "31.12.8.3 Enum class copy_­options"))
is set in options[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17284)

*Effects*: Before the first use of f and t:

- [(4.1)](#4.1)

If(options & copy_options::create_symlinks) != copy_options::none ||(options & copy_options::skip_symlinks) != copy_options::none then auto f = symlink_status(from) and if needed auto t = symlink_status(to).

- [(4.2)](#4.2)

Otherwise, if(options & copy_options::copy_symlinks) != copy_options::none then auto f = symlink_status(from) and if needed auto t = status(to).

- [(4.3)](#4.3)

Otherwise, auto f = status(from) and if needed auto t = status(to).

Effects are then as follows:

- [(4.4)](#4.4)

  If f.type() or t.type() is an implementation-defined
file type ([[fs.enum.file.type]](fs.enum.file.type "31.12.8.2 Enum class file_­type")), then the effects areimplementation-defined[.](#4.4.sentence-1)

- [(4.5)](#4.5)

  Otherwise, an error is reported as specified in [[fs.err.report]](fs.err.report "31.12.5 Error reporting") if
  * [(4.5.1)](#4.5.1)

exists(f) is false, or

  * [(4.5.2)](#4.5.2)

equivalent(from, to) is true, or

  * [(4.5.3)](#4.5.3)

is_other(f) || is_other(t) is true, or

  * [(4.5.4)](#4.5.4)

is_directory(f) && is_regular_file(t) is true[.](#4.5.sentence-1)

- [(4.6)](#4.6)

  Otherwise, if is_symlink(f), then:
  * [(4.6.1)](#4.6.1)

      If(options & copy_options​::​skip_symlinks) != copy_options​::​none then return[.](#4.6.1.sentence-1)

  * [(4.6.2)](#4.6.2)

      Otherwise if!exists(t) && (options & copy_options::copy_symlinks) != copy_options::none then copy_symlink(from, to)[.](#4.6.2.sentence-1)

  * [(4.6.3)](#4.6.3)

      Otherwise report an error as specified in [[fs.err.report]](fs.err.report "31.12.5 Error reporting")[.](#4.6.3.sentence-1)

- [(4.7)](#4.7)

  Otherwise, if is_regular_file(f), then:
  * [(4.7.1)](#4.7.1)

      If (options & copy_options​::​directories_only) != copy_options​::​none, then return[.](#4.7.1.sentence-1)

  * [(4.7.2)](#4.7.2)

      Otherwise, if (options & copy_options​::​create_symlinks)  != copy_options​::​none, then create a symbolic link to the
 source file[.](#4.7.2.sentence-1)

  * [(4.7.3)](#4.7.3)

      Otherwise, if (options & copy_options​::​create_hard_links) != copy_options​::​none,
 then create a hard link to the source file[.](#4.7.3.sentence-1)

  * [(4.7.4)](#4.7.4)

      Otherwise, if is_directory(t), then copy_file(from, to/from.filename(), options)[.](#4.7.4.sentence-1)

  * [(4.7.5)](#4.7.5)

      Otherwise, copy_file(from, to, options)[.](#4.7.5.sentence-1)

- [(4.8)](#4.8)

  Otherwise, ifis_directory(f) &&(options & copy_options::create_symlinks) != copy_options::none then report an error with an error_code argument
equal to make_error_code(errc​::​is_a_directory)[.](#4.8.sentence-1)

- [(4.9)](#4.9)

  Otherwise, ifis_directory(f) &&((options & copy_options::recursive) != copy_options::none || options == copy_options::none) then:
  * [(4.9.1)](#4.9.1)

If exists(t) is false, then create_directory(to, from).

  * [(4.9.2)](#4.9.2)

Then, iterate over the files in from, as if byfor (const directory_entry& x : directory_iterator(from)) copy(x.path(), to/x.path().filename(),
 options | copy_options::*in-recursive-copy*); where *in-recursive-copy* is a bitmask element of copy_options that is not one of the elements in [[fs.enum.copy.opts]](fs.enum.copy.opts "31.12.8.3 Enum class copy_­options").

- [(4.10)](#4.10)

  Otherwise, for the signature with argument ec, ec.clear()[.](#4.10.sentence-1)

- [(4.11)](#4.11)

  Otherwise, no effects[.](#4.11.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17381)

*Throws*: As specified in [[fs.err.report]](fs.err.report "31.12.5 Error reporting")[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17385)

*Remarks*: For the signature with argument ec, any
library functions called by the implementation shall have an error_code argument if applicable[.](#6.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L17390)

[*Example [1](#example-1)*:

Given this directory structure:

```
/dir1
  file1
  file2
  dir2
    file3

```

Calling copy("/dir1", "/dir3") would result in:

```
/dir1
  file1
  file2
  dir2
    file3
/dir3
  file1
  file2

```

Alternatively, calling copy("/dir1", "/dir3", copy_options​::​recursive) would result in:

```
/dir1
  file1
  file2
  dir2
    file3
/dir3
  file1
  file2
  dir2
    file3

```

â *end example*]
