<!--
{
  "availability" : [
    "macOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "FSKit",
  "identifier" : "/documentation/FSKit/FSVolume/Handler/renameItem(_:inDirectory:named:to:inDirectory:overItem:context:replyHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "FSKit"
    ],
    "preciseIdentifier" : "c:objc(pl)FSVolumeHandler(im)renameItem:inDirectory:named:toNewName:inDirectory:overItem:context:replyHandler:"
  },
  "title" : "renameItem(_:inDirectory:named:to:inDirectory:overItem:context:replyHandler:)"
}
-->

# renameItem(_:inDirectory:named:to:inDirectory:overItem:context:replyHandler:)

Renames an item from one path in the file system to another.

```
func renameItem(_ item: FSItem, inDirectory sourceDirectory: FSItem, named sourceName: FSFileName, to destinationName: FSFileName, inDirectory destinationDirectory: FSItem, overItem: FSItem?, context: FSContext, replyHandler reply: @escaping @Sendable (FSRenameItemResult?, (any Error)?) -> Void)
```

```
func renameItem(_ item: FSItem, inDirectory sourceDirectory: FSItem, named sourceName: FSFileName, to destinationName: FSFileName, inDirectory destinationDirectory: FSItem, overItem: FSItem?, context: FSContext) async throws -> FSRenameItemResult
```

## Parameters

`item`

The file system object being renamed.

`sourceDirectory`

The directory that currently contains the item to rename.

`sourceName`

The name of the item within the source directory.

`destinationName`

The new name of the item as it appears in `destinationDirectory`.

`destinationDirectory`

The directory to contain the renamed object, which may be the same as `sourceDirectory`.

`overItem`

The file system object if the destination exists, as discovered in a prior lookup. If this parameter is non-`nil`, mark `overItem` as deleted, so the file system can free its allocated space on the next call to [`reclaimItem(_:replyHandler:)`](/documentation/FSKit/FSVolume/Handler/reclaimItem(_:replyHandler:)). After doing so, ensure the operation finishes without errors.

`context`

An object that enables context-aware file system decisions throughout the operation.

`reply`

A block or closure to indicate success or failure. If renaming succeeds, pass an instance of [`FSRenameItemResult`](/documentation/FSKit/FSRenameItemResult) containing the [`FSFileName`](/documentation/FSKit/FSFileName) as it exists within `destinationDirectory`, the [`FSItem.Attributes`](/documentation/FSKit/FSItem/Attributes) of the renamed item, the updated [`FSItem.Attributes`](/documentation/FSKit/FSItem/Attributes) of the source directory, the updated [`FSItem.Attributes`](/documentation/FSKit/FSItem/Attributes) of the destination directory, the [`FSItem.Attributes`](/documentation/FSKit/FSItem/Attributes) of the overwritten item (if any), and the volume’s updated free space, along with a `nil` error. If renaming fails, pass the relevant error as the second parameter; FSKit ignores the [`FSRenameItemResult`](/documentation/FSKit/FSRenameItemResult) instance in this case. For an `async` Swift implementation, there’s no reply handler; simply return the result instance or throw an error.

## Discussion> Note: This method only implements plain `rename(2)` semantics. FSKit fails `renamex_np(2)` calls that pass flags, such as `RENAME_SWAP` or `RENAME_EXCL`, with `ENOTSUP` without calling this method.

Implement renaming along the lines of this algorithm:

- If `item` is a file:
  - If the destination file exists:
    - Remove the destination file.
  - If the source and destination directories are the same:
    - Rewrite the name in the existing directory.
  - Else:
    - Write the new entry in the destination directory.
    - Clear the old directory entry.
- If `item` is a directory:
  - If the destination directory exists:
    - If the destination directory isn’t empty:
      - Fail the operation with an error of <doc://com.apple.documentation/documentation/Foundation/NSPOSIXErrorDomain> and a code of `ENOTEMPTY`.
    - Else:
      - Remove the destination directory.
    - If the source and destination directories are the same:
      - Rewrite the name in the existing directory.
    - Else:
      - If the destination is a child of the source directory:
        - Fail the operation with an error.
      - Else:
        - Write the new entry in the destination directory.
        - Update `"."` and `".."` in the moved directory.
        - Clear the old directory entry.

---

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)