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

# renameItem(_:inDirectory:named:to:inDirectory:overItem: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?, replyHandler reply: @escaping @Sendable (FSFileName?, (any Error)?) -> Void)
```

## 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(_:)`. After doing so, ensure the operation finishes without errors.

`reply`

A block or closure to indicate success or failure. If renaming succeeds, pass the [`FSFileName`](/documentation/FSKit/FSFileName) as it exists within `destinationDirectory` and a `nil` error. If renaming fails, pass the relevant error as the second parameter; FSKit ignores any [`FSFileName`](/documentation/FSKit/FSFileName) in this case. For an `async` Swift implementation, there’s no reply handler; simply return the [`FSFileName`](/documentation/FSKit/FSFileName) or throw an error.

## Discussion

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)