Skip to content

Instantly share code, notes, and snippets.

@sardonyx001
Created March 3, 2026 04:33
Show Gist options
  • Select an option

  • Save sardonyx001/b415bb5f043977fac8fb9a9bbb2b9b12 to your computer and use it in GitHub Desktop.

Select an option

Save sardonyx001/b415bb5f043977fac8fb9a9bbb2b9b12 to your computer and use it in GitHub Desktop.
Claude Code session relocate command
description argument-hint allowed-tools
Move this session's conversation history to a new working directory path
<new-path> [--all] [--copy]
Bash(pwd:*), Bash(realpath:*), Bash(echo:*), Bash(ls:*), Bash(find:*), Bash(mkdir:*), Bash(mv:*), Bash(cp:*)

Relocate Claude Code session history to a new working directory.

Arguments: "$ARGUMENTS"

By default only the current session is relocated. Pass --all to relocate every session tied to this directory.

Follow these steps exactly, in order:

Step 1: Parse arguments

From "$ARGUMENTS", extract:

  • HAS_ALL_FLAG: true if --all appears anywhere
  • HAS_COPY_FLAG: true if --copy appears anywhere
  • TARGET_RAW: the remaining text after removing both flags

If TARGET_RAW is empty, print an error and stop:

Error: No target path provided. Usage: /relocate [--all] [--copy]

Step 2: Resolve the target path

Run:

realpath "$TARGET_RAW"

If realpath is not available, run:

cd "$TARGET_RAW" && pwd

Store the result as NEW_PATH. If the command fails, print an error and stop:

Error: Target path "$TARGET_RAW" does not exist or is not accessible.

Step 3: Validate target is a directory

Run:

ls "$NEW_PATH"

If this fails, stop:

Error: "$NEW_PATH" is not a directory.

Step 4: Compute session storage slugs

The slug for a path is that path with every non-alphanumeric character replaced by -.

Run:

OLD_SLUG=$(pwd | sed 's/[^a-zA-Z0-9]/-/g')
NEW_SLUG=$(echo "$NEW_PATH" | sed 's/[^a-zA-Z0-9]/-/g')
echo "OLD_SLUG=$OLD_SLUG"
echo "NEW_SLUG=$NEW_SLUG"

Step 5: Guard against same-directory relocation

If OLD_SLUG equals NEW_SLUG, stop:

Error: The current directory and the target resolve to the same session storage path. No action taken.

Step 6: Find sessions to relocate

If HAS_ALL_FLAG is false (default — current session only):

The current session is the most-recently-modified .jsonl file in the project storage directory (this session is actively being written to, so it always has the newest mtime).

Run:

CURRENT_SESSION=$(find ~/.claude/projects/"$OLD_SLUG" -maxdepth 1 -name "*.jsonl" -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | awk '{print $2}')
echo "CURRENT_SESSION=$CURRENT_SESSION"

On macOS where -printf is not available, use:

CURRENT_SESSION=$(ls -t ~/.claude/projects/"$OLD_SLUG"/*.jsonl 2>/dev/null | head -1)
echo "CURRENT_SESSION=$CURRENT_SESSION"

If no file is found, stop:

Error: No session history found for the current directory (~/.claude/projects/$OLD_SLUG/). Nothing to relocate.

Store FILE_LIST="$CURRENT_SESSION" and set FILE_COUNT=1.

If HAS_ALL_FLAG is true:

Run:

FILE_LIST=$(find ~/.claude/projects/"$OLD_SLUG" -maxdepth 1 -name "*.jsonl" 2>/dev/null)

If empty, stop:

Error: No session history found for the current directory (~/.claude/projects/$OLD_SLUG/). Nothing to relocate.

Count: FILE_COUNT=$(echo "$FILE_LIST" | wc -l | tr -d ' ')

Step 7: Create destination directory

Run:

mkdir -p ~/.claude/projects/"$NEW_SLUG"

Step 8: Transfer session files

Iterate over each file in FILE_LIST.

If HAS_COPY_FLAG is false (default — move):

for f in $FILE_LIST; do mv "$f" ~/.claude/projects/"$NEW_SLUG"/; done

Note: On the same filesystem, mv is atomic — the active session's open file descriptor continues writing to the same inode at its new location. If mv fails due to a cross-filesystem error, fall back to cp + rm for that file and warn the user.

If HAS_COPY_FLAG is true:

for f in $FILE_LIST; do cp -n "$f" ~/.claude/projects/"$NEW_SLUG"/; done

-n avoids overwriting any sessions already at the destination.

Step 9: Report result

Move, current session only:

Current session moved to ~/.claude/projects/$NEW_SLUG/

Open Claude Code from $NEW_PATH to continue. This active session is now writing to its new location.

Move, all sessions:

All $FILE_COUNT session(s) moved to ~/.claude/projects/$NEW_SLUG/

Open Claude Code from $NEW_PATH to continue. The active session is now writing to its new location.

Copy, current session only:

Current session copied to ~/.claude/projects/$NEW_SLUG/

Open Claude Code from $NEW_PATH to continue. Session history is now available at both locations.

Copy, all sessions:

All $FILE_COUNT session(s) copied to ~/.claude/projects/$NEW_SLUG/

Open Claude Code from $NEW_PATH to continue. Session history is now available at both locations. To remove the originals: rm ~/.claude/projects/$OLD_SLUG/*.jsonl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment