LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-27-2011, 09:11 AM   #1
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 48
Wildcard/regex in bash command


I am trying to use the following command to list files in a folder:

Code:
ls 2011-10-{01..20}.gz
However, this command does not run as expected for single digits days... it looks for 2011-10-1.gz, when i actually want to match 2011-10-01.gz

What am i missing here?
 
Old 10-27-2011, 09:25 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747
Try 'ls 2011-10-[0-3][0-9].gz'
 
Old 10-27-2011, 02:40 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
{..} is called brace expansion. The output can be zero-padded in bash version 4+, but not before that. You can use {0{1..9},{10..20}} for earlier versions.

http://wiki.bash-hackers.org/syntax/expansion/brace

Note though that brace expansion generates a list of all the possible patterns and passes them all to your command. This means that ls, in this case, is given a list of 20 possible filenames to search for, and will spit out an error on every non-matching name. This kind of thing makes brace expansion less than ideal for filename matching. Brace expansion is better used for generating sequences of strings for input into loops and such.

For filename matching, you should use globbing patterns instead, as allend just demonstrated.

http://mywiki.wooledge.org/glob

globs simply test existing filenames to see if they match the pattern, and expands to a list of the ones that match. So with globbing, ls only gets handed a list of the files that actually exist, and no errors will be generated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Can I dereference a wildcard in a command? wjtaylor Linux - General 1 03-31-2011 10:19 AM
Using the CHOWN command with a wildcard. Skillz Linux - General 14 01-25-2010 09:42 AM
bash in ubuntu: save the wildcard expansion beebop Programming 0 03-12-2006 01:19 AM
script for a command that won't accept wildcard value hemp4fuel Programming 4 05-24-2004 05:30 AM

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

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