A tool to extract and query Claude Code session information for easy restoration with claude -r.
# List all sessions
python3 ~/.claude/extract-sessions.py
# List sessions grouped by project
python3 ~/.claude/extract-sessions.py --by-project
# List sessions for a specific project
python3 ~/.claude/extract-sessions.py --project /path/to/project
# Limit results
python3 ~/.claude/extract-sessions.py --limit 10python3 ~/.claude/extract-sessions.py | \
jq -r '.sessions[] | select(.session_name | contains("keyword")) | .session_id'python3 ~/.claude/extract-sessions.py \
--project /home/user/my-project \
--limit 1 | \
jq -r '.sessions[0].session_id'python3 ~/.claude/extract-sessions.py --by-project | jq -r 'keys[]'python3 ~/.claude/extract-sessions.py --by-project --limit 5 | \
jq -r 'to_entries[] | "\(.key): \(.value | length) sessions"'Once you have the session ID, restore it with:
claude -r <session-id>{
"sessions": [
{
"session_id": "uuid",
"session_name": "First user message in session",
"project": "/absolute/path/to/project",
"timestamp": 1234567890123,
"date": "2025-10-24T11:16:54.970000"
}
]
}{
"/absolute/path/to/project": [
{
"session_id": "uuid",
"session_name": "First user message",
"timestamp": 1234567890123,
"date": "2025-10-24T11:16:54.970000"
}
]
}--claude-dir PATH: Path to Claude Code data directory (default:~/.claude)--project PATH: Filter sessions by project path--by-project: Group output by project--limit N: Limit number of sessions returned (per project when using--by-project)
The script correlates data from two sources:
history.jsonl: Contains user prompts with timestamps and project pathsdebug/*.txt: Session debug logs with UUIDs (session IDs)
It matches sessions to history entries using timestamp proximity (within 5 minutes) to determine:
- Session ID (UUID from debug filename)
- Session name (first user message from history)
- Project path (from history entry)
- Timestamp and human-readable date
Add to your .bashrc or .zshrc:
# List Claude sessions
claude-sessions() {
python3 ~/.claude/extract-sessions.py "$@"
}
# Find and restore Claude session by keyword
claude-find() {
local keyword="$1"
local session_id=$(python3 ~/.claude/extract-sessions.py | \
jq -r ".sessions[] | select(.session_name | contains(\"$keyword\")) | .session_id" | \
head -1)
if [ -n "$session_id" ]; then
echo "Found session: $session_id"
claude -r "$session_id"
else
echo "No session found matching: $keyword"
fi
}
# List recent sessions for current directory
claude-recent() {
local pwd=$(pwd)
python3 ~/.claude/extract-sessions.py --project "$pwd" --limit 10 | \
jq -r '.sessions[] | "\(.date) - \(.session_name[:80])"'
}# Install fzf first: https://github.com/junegunn/fzf
claude-browse() {
local session_id=$(python3 ~/.claude/extract-sessions.py --by-project | \
jq -r 'to_entries[] | .value[] | "\(.date) [\(.session_id)] \(.session_name)"' | \
fzf --height=50% --reverse --preview-window=hidden | \
grep -oP '\[([a-f0-9-]+)\]' | tr -d '[]')
if [ -n "$session_id" ]; then
claude -r "$session_id"
fi
}- Session correlation is based on timestamp proximity (5 minute window)
- Sessions without history entries won't have proper names
- Very old sessions may not correlate correctly if system time changed
- The first message may not always be the most descriptive session name
No sessions found:
- Check that
~/.claude/history.jsonlexists and has entries - Check that
~/.claude/debug/contains.txtfiles - Try running without filters first
Incorrect session names:
- The correlation window is 5 minutes; if clock was adjusted, matches may be off
- Some sessions may have been created without history entries
Permission errors:
- Ensure the script is executable:
chmod +x ~/.claude/extract-sessions.py - Ensure you have read access to
~/.claude/directory