I expected that:
$ rm *(1)*
would remove all files containing
(1) in the name. I was wrong. It removed all files in the directory.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
*(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:
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.
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
ls to test your pattern first if you aren't absolutely sure that's working :)