Can anyone explain why sudo ls with a wildcard doesn't work?

Solution 1:

One possibility is that you don't have permissions to access one or more of the directories in that path (/sites/servers/server_instance/logs). The wildcard expansion is carried out by your shell, and then the expanded paths are passed to the sudo command.

If your user doesn't have permissions, expansion wouldn't work in the first command. It would be run as-is (ls -ltr /sites/servers/server_instance/logs/access*), and there isn't a file literally named access*). If abc does have the required permissions for all the directories in the path, the second command, which didn't have any wildcards, would be untouched by your shell, and it would work fine.

$ sudo namei -lx foo/bar/baz
f: foo/bar/baz
drwxr-xr-x muru    muru    foo
drwx------ test    test    bar
drwxr-xr-x muru    muru    baz

$ sudo ls foo/bar/b*
ls: cannot access 'foo/bar/b*': No such file or directory

$ sudo -u test ls foo/bar/
baz

Solution 2:

You may have globbing disabled.

Look for something like set -f or set -o noglob before those lines in the script, or if in an interactive shell run echo $-; if there's an f in the output, globbing is disabled:

$ echo $-
fhimBH

To fix that, remove set -f or set -o noglob from the script, or if in an interactive shell run set +f or set +o noglob:

$ set -f
$ echo $-
fhimBH
$ ls access*
ls: cannot access access*: No such file or directory
$ set +f
$ echo $-
himBH
$ ls access*
access