LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem Using Wild Card With ls (https://www.linuxquestions.org/questions/linux-newbie-8/problem-using-wild-card-with-ls-4175450702/)

OtagoHarbour 02-18-2013 04:14 PM

Problem Using Wild Card With ls
 
I am using a gnome terminal on Ubuntu 11.10.

When I type

Code:

sudo ls -1 /var/www/Peter/test7/A-FILE-Name.cel
I get

Code:

/var/www/Peter/test7/A-FILE-Name.cel

However, when I type

Code:

sudo ls -1 /var/www/Peter/test7/*.cel
I get

Code:

ls: cannot access /var/www/Peter/test7/*.cel: No such file or directory
Anyone see what the problem is?

Thanks,
Peter.

evo2 02-18-2013 04:22 PM

Hi,

the shell glob (or expansion of the wild card) happens in the shell of the user before the sudo call. I guess the user doesn't have permission to view files in that directory.

Evo2.

RaviTezu 02-18-2013 09:41 PM

Hi OtagoHarbour,

Funny thing.. You are using 1(Number one)as an option(i.e -1) to the ls command. You have to use l(letter "l") for a long listing format. please read man page of ls.

I guess that was the reason. Please check that & reply.

Paste the output of
Quote:

sudo ls -l /var/www/Peter/test7/A-FILE-Name.cel

RaviTezu 02-18-2013 09:47 PM

Okay. Here it is... The output will look like this..

[user@machine ~]$ ls -l git-clones/scripts/python_scripts/README

-rw-rw-r--. 1 user user 69 Feb 15 12:26 git-clones/scripts/python_scripts/README

Where the first "-" represents a regular file.
rw- = Permissions for the owner
rw- = Permissions for the group user
r-- = Permissions for others
From "rw-rw-r--" represents the owner and group user have read,write permissions and where as other have read permissions.

Check this link.

shivaa 02-18-2013 10:48 PM

Quote:

Originally Posted by RaviTezu (Post 4894800)
Funny thing.. You are using 1(Number one)as an option(i.e -1) to the ls command. You have to use l(letter "l") for a long listing format. please read man page of ls.

Even with -1 option, it will list out all files that matches the globbing pattern. I doubt the filename used is wrong.

@OtagoHarbour:
Can you invoke following cmd and see if sub-directories exists and path of specified files is ok:-
Code:

~$ ls -R /var/

RaviTezu 02-18-2013 11:49 PM

Quote:

Originally Posted by shivaa (Post 4894814)
Even with -1 option, it will list out all files that matches the globbing pattern. I doubt the filename used is wrong.

That's what he get,when he ran the the ls with -1(digit 1) option..it just list the file.


@Shivaa ...Please check the Original post of OtagoHarbour

des_a 02-19-2013 12:14 AM

Be aware in this that a, "*" alone, refers to the whole filename and extension, unlike dos, where you need, "*.*".

evo2 02-19-2013 12:19 AM

Hi all,

from what I can tell the question is about sudo and shell globbing not options/flags/switches of ls (or even really ls: the same behaviour would be seen for any command).

It seems that what OP is trying to understand is the result of the fact that the shell can't glob files it can't see, where as ls can list such file because it is being run with higher privileges via sudo.

Perhaps we should just wait till the OP replies.

Evo2.

suicidaleggroll 02-19-2013 12:25 AM

Pretty sure evo2 nailed it in his first post, the rest of the replies have been either redundant or irrelevant. Best to just wait for the OP...

jpollard 02-19-2013 11:36 AM

As a last note... the -1 is a valid option - it means "use one column".

smbhandary 02-19-2013 10:36 PM

try escaping the *

sudo ls -1 /var/www/Peter/test7/\*.cel

Escaping wild card char * fixed similar issues faced using find and ls on AIX / Linux for me.

I cant explain why because I could not pin this behaviour to a OS / OS version.
Maybe I should try pinning it to a shell on an OS.

jpollard 02-20-2013 05:49 AM

It is the standard problem of handling parameters through the shell - "Where is it interpreted?".

Before the command line can be executed it must be parsed - and that parse breaks up the line into tokens.

If one of those tokens has shell metacharacters then it will be replaced - unless something prevents it.

The "something to prevent it" is an apostrophe quote ('), or a back slash (\). In either case, the escape character is removed (the apostrophe quoting as well as any double quote character ") and the resulting token is treated as a parameter.

In the "\*" above, the command line 'ls -1 /var/www/Peter/test7/*.cel' is then reprocessed by the shell started by sudo - and hence gets a different environment.

Now SOMETIMES that doesn't quite work as expected - nested shells can reinterpret, or not reinterpret depending on how the shell gets invoked. If the shell is invoked through the system or popen library functions, reinterpretation is more obvious. If, however, it is invoked via exec sequence then the parameters may not be reinterpreted - until they get used in a command line. If that command line is another shell (such as a script) then how it gets quoted again makes a difference- the script is attempting to pass a metacharacter to yet another shell - which may require that metacharacter to be escaped... but the escape character has already been removed...

This is also a source of security problems when using sudo/su and having it run a shell script. An escaped metacharacter can be passed to a more privileged script causing the script to do "bad things".

Consider this variant of the ls example: "sudo ls -1 /var/www/Peter/test\`rm -rf /home\`" (CAUTION: CAN DELETE THE HOME DIRECTORIES)

sudo attempts to prevent this via the "noexec" option, but that makes it hard to use a shell script as the command to execute via sudo... perl or python would be a better language in that case due to the more complete programming environment built into the interpreter. BTW, noexec would work for the ls example as ls is the only thing executed... but then, the previous example would not have worked either (I don't believe the shell is invoked when the noexec is used by sudo, I do know that use of the exec system call is blocked by a dummy function).

teatru bucuresti 02-20-2013 06:47 AM

Thank you, jpollard!

OtagoHarbour 02-23-2013 08:00 AM

Thank you to everyone for your help. My underlying problem was actually due to a logical bug in my PHP code. However I did not realise that the expansion of the wildcard happened before the sudo call. That was the issue in my test case.

shivaa 02-23-2013 09:09 AM

Happy to hear that. :)
Please Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.


All times are GMT -5. The time now is 09:25 PM.