<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileManager/linkItem(at:to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileManager(im)linkItemAtURL:toURL:error:"
  },
  "title" : "linkItem(at:to:)"
}
-->

# linkItem(at:to:)

Creates a hard link between the items at the specified URLs.

```
func linkItem(at srcURL: URL, to dstURL: URL) throws
```

## Parameters

`srcURL`

The file URL that identifies the source of the link. The URL in this parameter must not be a file reference URL; it must specify the actual path to the item. The value in this parameter must not be `nil`.

`dstURL`

The file URL that specifies where you want to create the hard link. The URL in this parameter must not be a file reference URL; it must specify the actual path to the item. The value in this parameter must not be `nil`.

## Discussion

Use this method to create hard links between files in the current file system. If `srcURL` is a directory, this method creates a new directory at `dstURL` and then creates hard links for the items in that directory. If `srcURL` is (or contains) a symbolic link, the symbolic link is copied and not converted to a hard link at `dstURL`.

Prior to linking each item, the file manager asks its delegate if it should actually create the link. It does this by calling the [`fileManager(_:shouldLinkItemAt:to:)`](/documentation/Foundation/FileManagerDelegate/fileManager(_:shouldLinkItemAt:to:)) method; if that method is not implemented it calls the [`fileManager(_:shouldLinkItemAtPath:toPath:)`](/documentation/Foundation/FileManagerDelegate/fileManager(_:shouldLinkItemAtPath:toPath:)) method instead. If the delegate method returns <doc://com.apple.documentation/documentation/Swift/true>, or if the delegate does not implement the appropriate methods, the file manager creates the hard link. If there is an error linking one out of several items, the file manager may also call the delegate’s [`fileManager(_:shouldProceedAfterError:linkingItemAt:to:)`](/documentation/Foundation/FileManagerDelegate/fileManager(_:shouldProceedAfterError:linkingItemAt:to:)) or [`fileManager(_:shouldProceedAfterError:linkingItemAtPath:toPath:)`](/documentation/Foundation/FileManagerDelegate/fileManager(_:shouldProceedAfterError:linkingItemAtPath:toPath:)) method to determine how to proceed.

> Handling Errors in Swift:
> In Swift, this method returns `Void` and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

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)