I wanted to delete all the files from directory except the one. I found my solution here. This solution is using a command

shopt -s extglob

I wanted to know what this command is exactly doing, some back end knowledge. I also added a comment on this answer but did not got a reply till now. As a new user to Ubuntu I am curious to know what this command is doing.


In simple terms, globbing refers to pattern matching. Bash uses simple globbing like, echo l* which expand to list of files in current directory that start with letter l. Of course , as you can guess, it's simple and limited.

Enter extglob. As you can guess, it stands for extended globbing. This option allows for more advanced pattern matching. From man bash:

extglob If set, the extended pattern matching features described
        above under Pathname Expansion are enabled.

And a little before that:

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
following sub-patterns:

             Matches zero or one occurrence of the given patterns
             Matches zero or more occurrences of the given patterns
             Matches one or more occurrences of the given patterns
             Matches one of the given patterns
             Matches anything except one of the given patterns

There's multitude of ways in which extglob can be used. Quite a few good examples are provided in Linux Journal and Greg's wiki.

  • Is this a permanent change? Is there a way to turn it off after I am done?
    – GC_
    Sep 16 '20 at 16:25
  • Right after I commented, I found shopt -u extglob to unset it. I still wonder how long it lasts.
    – GC_
    Sep 16 '20 at 16:28
  • 1
    If you want it permanent you need to add it into your ~/.bash_profile (or ~/.bashrc). The set option should be reset when closing the terminal or exiting the shell. Sep 16 '20 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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