<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FileDescriptor/duplicate(as:retryOnInterrupt:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System14FileDescriptorV9duplicate2as16retryOnInterruptA2CSg_SbtKF"
  },
  "title" : "duplicate(as:retryOnInterrupt:)"
}
-->

# duplicate(as:retryOnInterrupt:)

Duplicates this file descriptor and returns the newly created copy.

```
func duplicate(as target: FileDescriptor? = nil, retryOnInterrupt: Bool = true) throws -> FileDescriptor
```

## Parameters

`retryOnInterrupt`

Whether to retry the duplicate operation
if it throws [`interrupted`](/documentation/System/Errno/interrupted). The default is `true`.
Pass `false` to try only once and throw an error upon interruption.

## Return Value

The new file descriptor.

## Discussion

If the `target` descriptor is already in use, then it is first
deallocated as if a close(2) call had been done first.

File descriptors are merely references to some underlying system resource.
The system does not distinguish between the original and the new file
descriptor in any way. For example, read, write and seek operations on
one of them also affect the logical file position in the other, and
append mode, non-blocking I/O and asynchronous I/O options are shared
between the references. If a separate pointer into the file is desired,
a different object reference to the file must be obtained by issuing an
additional call to `open`.

However, each file descriptor maintains its own close-on-exec flag.

The corresponding C functions are `dup` and `dup2`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)