Skip to content

Instantly share code, notes, and snippets.

@NathanBaulch
Forked from obscurerichard/redis-scan.sh
Last active March 13, 2018 05:24
Show Gist options
  • Save NathanBaulch/a068f46ace464341b16630200869577b to your computer and use it in GitHub Desktop.
Save NathanBaulch/a068f46ace464341b16630200869577b to your computer and use it in GitHub Desktop.
A bash script that scans Redis keys by pattern using SCAN
#!/bin/bash
if [ "$#" -lt 1 ]
then
echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
echo "Usage: $0 <host> [port] [database] [pattern]"
exit 1
fi
host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${4:-\*}
cursor=-1
while [[ "$cursor" -ne 0 ]]; do
if [[ "$cursor" -eq -1 ]]
then
cursor=0
fi
reply=$(redis-cli -h "$host" -p "$port" -n "$database" --raw SCAN "$cursor" MATCH "$pattern" COUNT 1000)
cursor=${reply%%$'\n'*}
if [[ "$cursor" != "$reply" ]]
then
echo "${reply#*$'\n'}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment