9

Why use of *(1)* removes everything from a directory? Then how to select all files that has (1) in the middle of it's name?

The command used is rm *(1)* or rm -rf *(1)* (don't remember exactly). It was default Ubuntu shell.

0

1 Answer 1

13

If you have ksh-style extended globs enabled, e.g. with extglob set in Bash, then the pattern *(1)* indeed matches any filename. The *(...) syntax matches zero or more copies of the part in parenthesis, so any leading ones, if there are any. And then the * matches the rest.

If you were using a POSIX-like shell without extended globs, then that would be an error as the ( operator doesn't fit into the syntax there.

In zsh by default, I think that's the same as *1*, i.e. the parenthesis just work for grouping. Though if you have the kshglob shell option set, then it uses ksh rules.

I think recent versions of Ubuntu set extglob in Bash by default in interactive shells.

To match files with a literal (1) part in the middle, quote or escape that part or at least the parens: either *\(1\)*, or *"(1)"*.

6
  • Do all distros have extglob set in Bash?
    – Ya Y
    Dec 4, 2022 at 20:18
  • 2
    @YaY, probably not. You could use shopt extglob to see if it's enabled
    – ilkkachu
    Dec 4, 2022 at 20:25
  • @YaY not by default, as it can make a lot of bad things happen (see this question)
    – OrangeDog
    Dec 5, 2022 at 12:58
  • 1
    Related: Is it dangerous to always have extglob on?, and Why would I not leave extglob enabled in bash?, where the consensus seems to be that it's not really dangerous. Ok, I guess the question here shows a possible gotcha. Though anyway, the parenthesis would need to be quoted in a standard shell, and if one does that, extended globs aren't at all dangerous. Anyway, they're on by default in ksh and mksh.
    – ilkkachu
    Dec 5, 2022 at 20:51
  • @OrangeDog what question? No link there. But I had it by default.
    – Ya Y
    Dec 7, 2022 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .