I am pretty green when it comes to grep, can someone point out how I can get an array in bash of the list of snapshot names (NOTE: just the names) when I do a lxc info mycontainer ?

My current results are:

root@hosting:~/LXC-Commander# lxc info mycontainer --verbose
Name: mycontainer
Remote: unix:/var/lib/lxd/unix.socket
Architecture: x86_64
Created: 2017/05/01 21:27 UTC
Status: Running
Type: persistent
Profiles: mine
Pid: 23304
  eth0: inet   vethDRS01G
  eth0: inet6   fd9b:16e1:3513:f396:216:3eff:feb1:c997  vethDRS01G
  eth0: inet6   fe80::216:3eff:feb1:c997        vethDRS01G
  lo:   inet
  lo:   inet6   ::1
  Processes: 1324
  Memory usage:
    Memory (current): 306.63MB
    Memory (peak): 541.42MB
  Network usage:
      Bytes received: 289.16kB
      Bytes sent: 881.73kB
      Packets received: 692
      Packets sent: 651
      Bytes received: 1.51MB
      Bytes sent: 1.51MB
      Packets received: 740
      Packets sent: 740
  2017-04-29-mycontainer (taken at 2017/04/29 21:54 UTC) (stateless)
  2017-04-30-mycontainer (taken at 2017/04/30 21:54 UTC) (stateless)
  2017-05-01-mycontainer (taken at 2017/05/01 21:54 UTC) (stateless)

With my ultimate goal of simply containing an array such as: 2017-04-29-mycontainer 2017-04-30-mycontainer 2017-05-01-mycontainer

1 Answer 1


With lxc list --format=json you get a JSON document with a lot of information about all the various available containers.

lxc list mycontainer --format=json limits this to the containers whose names start with the string mycontainer (use 'mycontainer$' for an exact match).

Parsing JSON is generally safer than parsing a text document that is almost free form.

To extract the names of the snapshots using jq:

$ lxc list mycontainer --format=json | jq -r '.[].snapshots[].name'

This will give you a list like


To put this into an array in bash:

snaps=( $( lxc list mycontainer --format=json | jq -r '.[].snapshots[].name' ) )

Just be aware that if you do this, snapshot names with characters that are special to the shell (*?[) will cause file name globbing to happen. You can prevent this with set -f before the command (and set +f after).

If you just want to loop over the snapshots:

lxc list mycontainer --format=json | jq -r '.[].snapshots[].name' |
while read snap; do
   # do something with "$snap"
  • nice mate!. I have to install jq tho ;) Will let ya know. Yeah, the nameshot names will always be named YYYY-MM-DD-mycontainer :)
    – Kevin
    May 2, 2017 at 11:46
  • strangely: snaps=( $( lxc list mycontainer --format=json | jq -r '.[].snapshots[].name' ) ) doesn't return anything, yet without assigning it to a variable returns me the full snapshot name (tho, it includes the container name as well... like: mycontainer/2017-05-01-mycontainer ...
    – Kevin
    May 2, 2017 at 11:49
  • 1
    @Kevin check the contents of the array with echo "${snaps[@]}". The installation of lxc that I use seems to return the names without the container name prepended... Odd.
    – Kusalananda
    May 2, 2017 at 11:51
  • 1
    @Kevin Same as me. Well, if you have one snapshot name in a variable $snap, then snap="${snap#*/}" will remove whatever is in front of the / (including the /).
    – Kusalananda
    May 2, 2017 at 11:55
  • 1
    Actually. It may workout better with the full name anyways. I'll be using this to cycle through the snapshots to delete the oldest (out of say 7 days worth) anyways :D
    – Kevin
    May 2, 2017 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .