Instance Method

linkItem(at:to:)

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

Declaration

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.

Return Value

true if the hard link was created or false if an error occurred. This method also returns false if a file, directory, or link already exists at dstURL.

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:) method; if that method is not implemented it calls the fileManager(_:shouldLinkItemAtPath:toPath:) method instead. If the delegate method returns 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:) or fileManager(_:shouldProceedAfterError:linkingItemAtPath:toPath:) method to determine how to proceed.

See Also

Creating Symbolic and Hard Links

func createSymbolicLink(at: URL, withDestinationURL: URL)

Creates a symbolic link at the specified URL that points to an item at the given URL.

func createSymbolicLink(atPath: String, withDestinationPath: String)

Creates a symbolic link that points to the specified destination.

func linkItem(atPath: String, toPath: String)

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

func destinationOfSymbolicLink(atPath: String) -> String

Returns the path of the item pointed to by a symbolic link.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software