How to get value by redis-cli keys

weiwei picture weiwei · Jun 29, 2017 · Viewed 15.9k times · Source

I want get value by redis-cli keys

This is work

redis-cli keys number_* | xargs redis-cli del

But this is not work

redis-cli keys number_* | xargs redis-cli get

Answer

Itamar Haber picture Itamar Haber · Jun 29, 2017

The difference between DEL and GET, in this context, is that the former is variadic (i.e. accepts one or more arguments) whereas the latter isn't (one and only one key name is expected).

To solve this you can choose one of the following:

  1. Use the -L switch with xargs, i.e.: redis-cli keys number_* | xargs -L 1 redis-cli get
  2. Use MGET, i.e.: redis-cli keys number_* | xargs redis-cli mget

Important warning: KEYS is a dangerous command as it may block the server for a long time - do not use it in production!