LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2013, 04:14 PM   #1
OtagoHarbour
Member
 
Registered: Oct 2011
Posts: 332

Rep: Reputation: 3
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.
 
Old 02-18-2013, 04:22 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
3 members found this post helpful.
Old 02-18-2013, 09:41 PM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
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
 
Old 02-18-2013, 09:47 PM   #4
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
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.
 
Old 02-18-2013, 10:48 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by RaviTezu View Post
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/
 
Old 02-18-2013, 11:49 PM   #6
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Quote:
Originally Posted by shivaa View Post
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

Last edited by RaviTezu; 02-18-2013 at 11:54 PM.
 
Old 02-19-2013, 12:14 AM   #7
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441
Blog Entries: 43

Rep: Reputation: 36
Be aware in this that a, "*" alone, refers to the whole filename and extension, unlike dos, where you need, "*.*".
 
Old 02-19-2013, 12:19 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 02-19-2013, 12:25 AM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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...
 
Old 02-19-2013, 11:36 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
As a last note... the -1 is a valid option - it means "use one column".
 
Old 02-19-2013, 10:36 PM   #11
smbhandary
LQ Newbie
 
Registered: Jan 2013
Distribution: RHEL and AIX ( sort of )
Posts: 18

Rep: Reputation: Disabled
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.

Last edited by smbhandary; 02-20-2013 at 12:42 AM.
 
Old 02-20-2013, 05:49 AM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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).

Last edited by jpollard; 02-20-2013 at 05:55 AM.
 
Old 02-20-2013, 06:47 AM   #13
teatru bucuresti
LQ Newbie
 
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
Thank you, jpollard!
 
Old 02-23-2013, 08:00 AM   #14
OtagoHarbour
Member
 
Registered: Oct 2011
Posts: 332

Original Poster
Rep: Reputation: 3
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.
 
Old 02-23-2013, 09:09 AM   #15
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Postfix + Procmail + Wild Card Subdomains with local delivery sushantchawla2005 Linux - Server 0 11-24-2012 10:44 AM
Bash Wild Card rivacom Linux - General 15 08-05-2010 07:13 AM
How to send a string to function containing wild card paragkalra Programming 2 04-08-2010 12:51 AM
Help SSH into logfile and search with array loop of wild card characters gatorpower Programming 1 12-09-2009 10:20 AM
How to count the number of files recursively with wild card redir Linux - Newbie 5 02-28-2006 11:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration