Search code examples
redisredis-cli

`redis-cli keys *` shows empty array, but `redis-cli keys my_key` shows the key


I have a redis server running in a Docker container. I push values to a list in there from an outside script. When I'm in the redis container, however, redis-cli keys * returns (empty array). This isn't an issue with selecting the correct database, I've tried that. See the following terminal output:

/redis_data $ redis-cli info keyspace
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
/redis_data $ redis-cli keys *
(empty array)
/redis_data $ redis-cli select 0
OK
/redis_data $ redis-cli keys *
(empty array)
/redis_data $ redis-cli keys log_list # this is the key I've been pushing to
1) "log_list"
/redis_data $ redis-cli keys dne # a key I know doesn't exist
(empty array)

Solution

  • I discovered that wrapping the pattern in quotes makes it function correctly. This leads me to believe the shell is expanding my * as a shell glob before redis can see it as a pattern. See the following terminal output:

    /redis_data $ ls
    dump.rdb
    /redis_data $ redis-cli keys *
    (empty array)
    /redis_data $ redis-cli keys "*"
    1) "log_list"
    /redis_data $ echo *
    dump.rdb
    /redis_data $ echo "*"
    *
    /redis_data $ redis-cli
    127.0.0.1:6379> keys *
    1) "log_list"
    127.0.0.1:6379> keys "*"
    1) "log_list"
    127.0.0.1:6379> exit
    

    Observe my test with echo that confirms my theory. Functionally, my redis-cli keys * was getting executed as redis-cli keys dump.rdb, which of course gave an empty array. Interestingly, this error was only possible because I had exactly one file in my working directory. If I had more than one, I would've gotten (error) ERR wrong number of arguments for 'keys' command from redis-cli keys *, which might've tipped me off to the issue.

    Also worth noting is that the issue can be avoided by running redis-cli and then doing redis commands from the 127.0.0.1:port> prompt where no shell globbing is done, as seen above.