LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-24-2012, 10:48 AM   #1
cwilliams6419
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
Unhappy BASH Wildcard * and ?


Can someone please explain why I get these results when I execute the following commands:

for ((i=0; i<=200; i++)); do $(touch file.$i);done
rm file.*[0-9][0-9]

I end up with file.0 - file.9



for ((i=0; i<=200; i++)); do $(touch file.$i);done
rm file.?[0-9][0-9]

I end up with file.0 - file.99


Thank you
 
Old 02-24-2012, 10:56 AM   #2
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
* matches any number of characters, including 0 characters
? matches ONE character, exactly 1.

Code:
$ ls
00 010 01210 0123210 12345 54321
$ ls 0*0
00 010 01210 0123210
$ ls 0?0
010
You're getting the results you're getting because * can match any number of characters, including 0. So your "rm file.*[0-9][0-9]" is the same (in your special case) as running "rm file.[0-9][0-9] file.[0-9][0-9][0-9]". In other words, it removes all files with both 2 digits and 3 digits after the decimal. This is why you're only left with files that have one digit after the decimal.

Meanwhile, your "rm file.?[0-9][0-9]" will ONLY match files with 3 digits after the decimal, it would be like running "rm file.[0-9][0-9][0-9]". This is why you're left with files that have both one and two digits after the decimal.

Last edited by suicidaleggroll; 02-24-2012 at 10:58 AM.
 
Old 02-24-2012, 11:15 AM   #3
cwilliams6419
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you... got it.
 
Old 02-24-2012, 12:09 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution
 
Old 02-24-2012, 12:19 PM   #5
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by cwilliams6419 View Post
...
rm file.*[0-9][0-9]

rm file.?[0-9][0-9]
Your first 'rm' command asks to remove items:
  • whose name contains "file"
  • followed by a period "."
  • followed by zero or more characters "*"
  • followed by a digit "[0-9]"
  • followed by another digit "[0-9]"

Your second 'rm' command asks to remove items:
  • whose name contains "file"
  • followed by a period "."
  • followed by any one character "?"
  • followed by a digit "[0-9]"
  • followed by another digit "[0-9]"

Command line wild cards follow the rules of what are called "globbing." Check out man 3 glob for the programmer interface if you are interested. Check out man 7 glob for the shell command interface.

Good luck,
~~~ 0;-Dan

DON'T FORGET -- mark the thread solved if you have your answers.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS wildcard help conflicker Linux - Newbie 3 02-07-2012 10:56 AM
bash wildcard expansion length issue scazz Linux - Newbie 2 11-01-2011 12:32 PM
Wildcard/regex in bash command logicalfuzz Linux - General 2 10-27-2011 02:40 PM
Wildcard Subdomains anjanesh Ubuntu 1 07-17-2009 06:54 PM
bash in ubuntu: save the wildcard expansion beebop Programming 0 03-12-2006 01:19 AM

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

All times are GMT -5. The time now is 02:44 PM.

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