Cannot expand asterisk without proper permission
Solution 1
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.
Solution 2
In some rare cases you need to double check that you have not disabled globbing for your shell which could give you No such file or directory
when attempting to list files with wildcard (*
).
For example:
$ ls /tmp/sit/build/*
/tmp/sit/build/file
$ set -f
$ ls /tmp/sit/build/*
ls: cannot access /tmp/sit/build/*: No such file or directory
$ set +f
$ ls /tmp/sit/build/*
/tmp/sit/build/file
Check help set
for more info.
Related videos on Youtube
imagineerThis
Updated on September 18, 2022Comments
-
imagineerThis over 1 year
Can someone explain to me the following?
$ ls -ld /temp/sit/build/ dr-xr-s--- 3 asdf qwer 4096 Jan 31 2012 /temp/sit/build/ $ ls -ld /temp/sit/build/* ls: /temp/sit/build/*: Permission denied
So apprently, I can't use the asterisk here. I've tried it with a sudo command and I get a "no such file" error rather than "permission denied"...
sudo ls -l /temp/sit/build/* ls: /temp/sit/build/batch*: No such file or directory
but it finally works if I don't use the *
sudo ls -l /temp/sit/build/ total 4 dr-xr-s--- 11 asdf qwer 4096 Oct 3 23:31 file