Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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