Wildcard expansions are done by the shell, not by 'ls'. In other words, they don't wait for 'sudo' to take effect.
When your shell doesn't have permissions to see the files inside a protected directory, it cannot expand the wildcard, so the result is that 'ls' is being asked to display a file literally named *.tar
. (As mentioned, 'ls' doesn't do wildcard expansion on its own.)
As a general solution, have 'sudo' run another copy of the shell first – that will handle the wildcard expansion with root privileges:
sudo sh -c 'ls /var/bleh/*.tar'
(sudo $SHELL -c '...'
might be more general, but 'sh' does the job.)
Certain programs might have their own options such as --include=...
which do expand wildcards in the program itself. Unfortunately, 'ls' only has --ignore='*.tar'
but no option to only include specific names.
/var/opt/gitlab/backups/*.tar
is evaluated by your current shell, BEFORE the sudo, and you don't have permission to see that directory.