Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 10-27-2011, 09:11 AM   #1
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 47
Wildcard/regex in bash command

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

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
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,601

Rep: Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
{..} 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.

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.

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.


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 > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:38 AM.

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