A Shell Glob Expands Before sudo Runs, So sudo rm on a Root-Only Directory Can Delete Nothing

In progress Linux

In sudo rm -rf /etc/cni/net.d/*, the * is expanded by your shell, as your user, before sudo starts. If your user can't list the directory (/etc/cni/net.d is drwx------ root), the glob matches nothing, bash passes the literal string /etc/cni/net.d/* to rm, and rm -f ignores a path that doesn't exist. No error, nothing deleted.

Run the Whole Command as Root

sudo find /etc/cni/net.d -mindepth 1 -delete        # find does the listing as root
sudo sh -c 'rm -rf /etc/cni/net.d/*'                # or let a root shell expand the glob
sudo ls -A /etc/cni/net.d                           # verify: nothing listed

Globs also skip dotfiles by default, another way for * to leave files behind (here .kubernetes-cni-keep, which is harmless).

Without -f, rm would have printed No such file or directory: -f turns a mistake into silence. The same expansion happens with redirections (sudo echo x > /etc/file writes as your user), which is why | sudo tee exists.

References

Review cards

Flashcard Why can `sudo rm -rf /etc/cni/net.d/*` delete nothing and print nothing?

Your own shell expands * before sudo runs. It can't list the root-only directory, so the glob stays literal, and rm -f silently ignores a path that doesn't exist.

Flashcard How do you delete the contents of a root-only directory reliably?

Let root do the listing: sudo find DIR -mindepth 1 -delete, or sudo sh -c 'rm -rf DIR/*'. Then verify with sudo ls -A DIR.