Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I expected that:

$ rm *(1)*

would remove all files containing (1) in the name. I was wrong. It removed all files in the directory.

Why?

share|improve this question
3  
This is one of those "Oops" moments, isn't it? –  nneonneo Mar 19 at 8:43
    
Fortunately, it was in the Downloads directory, so no big loss ... –  Eric Wilson Mar 19 at 10:18
4  
Whenever I use rm with a pattern, I always precede it by echo before issuing the actual command. The habit saved me more than once (ever since, as a 6 year old or so, confusing the difference between DEL A: *.* and DEL *.* A:). –  gerrit Mar 19 at 12:38

2 Answers 2

up vote 49 down vote accepted

From man bash:

*(pattern-list)
                 Matches zero or more occurrences of the given patterns

You have a glob expression which matches files beginning with zero or more 1s - which is all files.

One simple way to disable this globbing behaviour is to \ escape the parentheses:

rm *\(1\)*

Otherwise you can use shopt -u extglob to disable the behaviour and shopt -s extglob to re-enable it:

shopt -u extglob
rm *(1)*
shopt -s extglob

Note that as Stephane says, extglob is enabled by bash-completion so disabling it may cause completion functions not to work properly.

share|improve this answer
7  
Note that extglob is not on by default but is turned on by bash_completion if you have that installed and enabled. bash doesn't have local scope for options like zsh does. –  Stéphane Chazelas Mar 18 at 13:18
    
Also note that bash-4.3 has a regression in that *(1)* also expands hidden files. –  Stéphane Chazelas Mar 18 at 13:34
    
@StephaneChazelas Do you have a link for the bug relating to that regression? Thanks –  Basic Mar 18 at 23:30
2  
@Basic, here you go. Chet hasn't replied yet. –  Stéphane Chazelas Mar 19 at 7:07

This is probably related to the extglob shell option. If I turn it off, the pattern produces an error message:

martin@dogmeat:~$ shopt -u extglob
martin@dogmeat:~$ shopt extglob
extglob         off
martin@dogmeat:~$ echo *(1)*
bash: syntax error near unexpected token `('

If I turn it on, it indeed seems to match everything. The manpage documents these patterns, I think they are related:

   If the extglob shell option is enabled using the shopt builtin, several
   extended  pattern  matching operators are recognized.  In the following
   description, a pattern-list is a list of one or more patterns separated
   by a |.  Composite patterns may be formed using one or more of the fol‐
   lowing sub-patterns:

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns

I don't see any documentation that specifies what parenthesis without a leading character do. Anyway, you can circumvent the issue by quoting the parens:

martin@dogmeat ~ % echo *\(1\)*
A(1)b

Also, use echo or ls to test your pattern first if you aren't absolutely sure that's working :)

share|improve this answer
    
FYI: { is a brace, ( is a paren (or round bracket). –  Mikel Mar 18 at 13:29
    
Ah, thanks. I'm not a native English speaker so I tend to confuse them :) –  Martin von Wittich Mar 18 at 13:38
6  
In British: { = curly bracket, ( = round bracket, [ = square bracket. In American: { = brace, ( = paren, [ = bracket. A bit confusing. If you ever need to check what something's called, Jargon File - ASCII is quite helpful. –  Mikel Mar 18 at 13:39
4  
@Mikel Thanks, I also checked here so now I know about flower brackets and the squirrelly ones! –  Amphiteóth Mar 18 at 16:00
    
In Indian English, { is a flower bracket, [ is a square bracket, and ( is a round bracket or more commonly just bracket. :-) –  ShreevatsaR Mar 19 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.