LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-28-2007, 07:15 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
using wildcards


say i want to list all files that are 2 characters long in the current directory, using wildcards. i could do something like 'ls ??', correct? this works fine but when i specify a directory it doesnt work the same.

from the man page it says for the synopsis: /usr/bin/ls [-aAbcCdeEfFghHilLmnopqrRstuvVx1@] [file...]

so the directory is the last argument, correct? if i do 'ls ?? /etc/' i get no results. however if i change to this directory and run 'ls ??' i do get the output as expected.

so, surprisingly, i dont know how to use the ls command! if someone could shed some light on this embarrassing topic id appreciate it. thank you
 
Old 01-28-2007, 08:17 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Put the directory first.

Example:

ls /bin/??

Return:

cp dd df ed ln ls mv ps rm sh su vi

Last edited by bigrigdriver; 01-28-2007 at 08:18 PM.
 
Old 01-28-2007, 08:18 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Note that the ls command isn't interpreting patterns in filenames at all - it never even sees them. Pattern expansion is done by the shell, which pre-expands patterns to lists of file names which match the pattern, and then passes the expanded list to whatever program is invoked.

The good news about this method is that it ensures consistency in pattern expansion, unlike with DOS which relies on each program implementing pattern expansion.

The bad news is that there are many different shell implementations, and so there is scope for some differences in how pattern expansion is done. Fortunately, most common shells implement patterns in broadly compatible ways.

Bash, which is probably your shell if you are using a modern Linux distro (find out by typing "ps -p $$" in your shell), has several modes for pattern expansion, which you can switch between.

Basically, it uses glob expansion, where ? is a single character wildcard, * matches any string, and [chars] can be used to specify from a list of characters. There are some more options too. See the "Pathname Expansion" section of the bash manual page for details.

Regarding this specific case of
Code:
ls ?? /etc
It's curious.

To find out what is happening, I'd recommand adding the -d option, which prevents ls from looking inside directories whicg are passed to it.

I cannot replicate the behaviour you describe - with ?? not expanding if it is followed by /etc:
Code:
matthew@chubby:~/tmp/testdir$ pwd
/home/matthew/tmp/testdir
matthew@chubby:~/tmp/testdir$ ls -d ?? /etc
ls: ??: No such file or directory
/etc/
matthew@chubby:~/tmp/testdir$ touch aa ab ac
matthew@chubby:~/tmp/testdir$ ls -d ??
aa  ab  ac
matthew@chubby:~/tmp/testdir$ ls -d ?? /etc
aa  ab  ac  /etc/
...seems to work for me. If you have the failglob shopt set and ?? doesn't match any files, the ls won't be executed, so you might just see the error message, if there are no filenames with two characters in the current working directory.
 
Old 01-28-2007, 08:19 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
cant believe i didnt try this. thanks for your time.

edit: posted as i did, matt! i was aware of the shell expanding the wildcards (didnt know DOS was the same way, but i should have assumed that, heh). regarding the -d it is just what i needed, too.. thanks for your time as well. i understand what the output is doing in your code portion. i should have just simply thought about it--sometimes you skip the easiest and most obvious solutions.

Last edited by nadroj; 01-28-2007 at 08:30 PM.
 
Old 01-28-2007, 08:38 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by nadroj
(didnt know DOS was the same way, but i should have assumed that, heh)
No, DOS is passing patterns to programs, which each must implement pattern expansion. Unix is the other way round - the shell pre-expands the list and passed the expanded list to the programs... the programs don't need to expand anything.

Having said that, there are programs which accept patterns as parameters or options (find -name ...) springs to mind. In this case, it is necessary to prevent the shell from interpreting the pattern, by quoting it somehow.
 
Old 01-28-2007, 08:39 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
sorry i meant 'wasnt'
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with shell script and wildcards BlueNoteMKVI Programming 3 10-04-2006 07:18 AM
Handling wildcards... artificialGekko Linux - General 5 03-24-2005 08:29 PM
CLI wildcards ShakyJake Linux - Newbie 4 02-28-2005 03:42 PM
Wildcards dazdaz Linux - Newbie 3 01-23-2005 05:33 AM
Wildcards in kernel Squall Linux - General 1 04-20-2004 05:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:12 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