# lxc exec mycontainer rm -r /home/myuser/mydir

gives me

Error: unknown shorthand flag: 'r' in -r
# lxc exec mycontainer "bash -c 'rm /home/myuser/mydir -r'"

just silently exits without removing the directory.

How can I delete a directory within the container?

  • Also lxc file does not have a delete command – exebook Jan 23 at 17:13

If you're using Docker, you can:

docker container exec mycontainer -it bash


docker container exec mycontainer -it /bin/bash

Enter inside the container and remove the directory or, depending of how you've built it, you can run the command directly:

docker container exec mycontainer sh -c 'rm /path'

By the way, I guess nobody uses lxc directly, only through Docker, Rancher or other container orchestration software.

| improve this answer | |
  • Well I can understand the reasoning, but we are using LXD directly (not LXC). Number of questions related to LXC/LXD definitely shows that "nobody" is slightly exaggerated. Docker: 640 questions, LXC: 380, LXD: 210. – exebook Jan 23 at 18:37

Just use --

lxc exec mycontainer -- rm -r /home/myuser/mydir
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.