Cannot expand asterisk without proper permission
The shell that's doing the expansion of the *
wildcard is the shell where you type it. If the shell has the permission to read the list of files in the directory, then it expands /temp/sit/build/*
to /temp/sit/build/file
, and runs sudo
with the arguments ls
, -l
and /temp/sit/build/file
. If the shell is unable to find any match for /temp/sit/build/*
(whether it's because there are no matches, or because the shell has no permission to see the matches), then it leaves the pattern alone, and sudo
is called with the arguments ls
, -l
and /temp/sit/build/*
.
Since there is no file called /temp/sit/build/*
, the ls
command complains if you pass it that name. Recall that ls
doesn't expand wildcards, that's the shell's job.
If you want wildcard expansion to happen in a directory where you don't have read permission, then the expansion must happen in a shell that's started by sudo
instead of in the shell that calls sudo
. sudo
doesn't automatically start a shell, you need to do that explicitly.
sudo sh -c 'ls -l /temp/sit/build/*'
Here, of course, you can do sudo ls -l /temp/sit/build/
instead, but that doesn't generalize to other patterns.