sudo rm -rf /opt/nextcloud/* is interpreted by the shell running as your regular user. The shell tries to expand
sudo even runs. If for some reason
* cannot be expanded, then the command becomes:
sudo rm -rf /opt/nextcloud/* where
sudo (and then
/opt/nextcloud/* literally with the literal
* and no further expansion occurs; this is POSIX behavior;
/opt/nextcloud/* obviously does not exist.
sudo rm -rf in some circumstances (e.g. in Bash when the
nullglob option is set).
There are other possibilities (e.g. compare
failglob in Bash) but they seem not to match your case.
In any case the
-f option of
rm gets relevant. It means
Do not prompt for confirmation. Do not write diagnostic messages or modify the exit status in the case of no file operands, or in the case of operands that do not exist. […]
Effectively you provided an "operand that does not exist" or "no file operands", so the command did nothing and succeeded silently.
My hypothesis is the non-elevated shell couldn't expand
/opt/nextcloud/* because the regular user has no read permissions on
/opt/nextcloud/. Another possibility is you made a typo in
/opt/nextcloud/* and didn't spot this because there was no error message. There was no error message because this is how
rm -f works.
sudo ls /opt/nextcloud/ involves no expansion by the shell. The command run
ls as root. The
ls itself had access to the specified directory and its content, therefore it worked.
sudo su - you were in a shell running as root. This shell was able to expand
rm -rf /opt/nextcloud/* worked as you expected.
rm -rf, in order to visualize what I'm going to delete.