5

I have a silly problem

usr01@srvlnx01:~$ sudo ls /var/opt/gitlab/backups/
1685014011_2023_05_25_13.2.3_gitlab_backup.tar  1685016443_2023_05_25_13.2.3_gitlab_backup.tar  tmp

but

usr01@srvlnx01:~$ sudo ls /var/opt/gitlab/backups/*.tar
ls: cannot access '/var/opt/gitlab/backups/*.tar': No such file or directory

I do something silly probably.

2
  • That does work for me. What shell does root have set?
    – davidgo
    May 25 at 12:42
  • 5
    The problem is that /var/opt/gitlab/backups/*.tar is evaluated by your current shell, BEFORE the sudo, and you don't have permission to see that directory. May 26 at 3:13

2 Answers 2

15

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.

0
4

The other answer does a great job of telling you why your specific solution isn't working so I won't harp on that.

You should use a robust command like find

sudo find /var/opt/gitlab/backups -type f -name '*.tar'

Notice -type f because it's important, it makes sure to only match files. There's nothing illegal about naming a folder mydir.tar and your ls idea would match such a dir.

1
  • I use find as well when I delete old backup files. May 26 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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