Questions tagged [extglob]

Extglob (extended pattern matching) is a form of wildcards supported by bash. With extglob a pattern can be almost as powerful as a regular expression.

Extglob must be enabled before using:

$ shopt -s extglob

Several extended pattern matching operators are then recognized.

?(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

References:

20 questions
16
votes
3 answers

Pattern match does not work in bash script

Using the pattern match !("file1") does not work within a bash script but will work on the command line. For example: ls !("file1"|"file2") This will list all files in directory except file1 and file2. When that line is executed in a script this…
Ogden
  • 171
  • 5
6
votes
1 answer

How does negative matching work in extglob in parameter expansion

Problem The behaviour of !(pattern-list) does not work the way I would expect when used in parameter expansion, specifically ${parameter/pattern/string} Input a="1 2 3 4 5 6 7 8 9 10" Test cases $ printf "%s\n"…
123
  • 10,778
  • 2
  • 22
  • 45
4
votes
1 answer

How to convert regex into an extglob expression?

I'd like to convert regular expression into glob I was looking on jakarta oro But i can't find method that suits my needs. That it compiles regular expression and returns its glob equivalent They are both Type-3 grammars, so in theory it should be…
Rob
  • 708
  • 8
  • 27
3
votes
2 answers

bash extglob range-or-value matching same file name twice

Given these files: $ ls file1 file2 file21 file3 fileFoo I expected this globbing pattern (bash with extglob enabled): $ ls file?({1..2}|Foo) to output: file1 file2 fileFoo but instead I got this with fileFoo listed twice: $ ls…
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2
votes
1 answer

How to remove all files except the ones that matches a pattern along several number of directories

lets say I have a my_dirs/ directory, insdie that directory I have several parallel subdirectories which has several files and I want to delete all of them except the ones that have the substring '.regions' this is my parent directory content: this…
Valentin
  • 399
  • 2
  • 10
2
votes
1 answer

Negate in bash extended globs does not work

I am trying to list some selective files but want to exclude atop_20210428, but the following extended glob atop_20210@(3|4)*[0-4]*!(8)* does not exclude the file atop_20210428, what is the correction required in that?. [root@server atop]# ls -lh…
vjwilson
  • 754
  • 2
  • 14
  • 30
2
votes
2 answers

Deleting everything, except for two files

I would like to delete everything in a folder, including folders, except for two files. To do so, why using this script: #!/usr/bin/env bash shopt -s extglob rm !(file1|file2) Which works, but when i try to execute within a case: #!/usr/bin/env…
user6332096
1
vote
1 answer

bash extglob pattern matching breaks when enclosed in a function

This works shopt -s extglob find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \; shopt -u extglob This returns an error syntax error near unexpected token `(' function test { shopt -s extglob find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec…
B H
  • 111
  • 1
  • 3
1
vote
1 answer

Pattern works in interactive shell but not in script

In my interactive shell I can store a pattern in a variable $ targets="?(pcm52|pcm61)" $ ls -l config/devdesc.$targets -rw-r--r-- 1 kfa kfa 113 Dec 16 13:43 config/devdesc.pcm52 -rw-r--r-- 1 kfa kfa 14 Dec 16 13:44 config/devdesc.pcm61 But if I…
Kjeld Flarup
  • 1,471
  • 10
  • 15
1
vote
0 answers

bash script doesn't act like shell

Here is my script: #!/bin/bash x="aabcaa" y=${x##*(a)} echo $y It outputs aabcaa when I expect bcaa. When I try the same commands on a bash shell (got with bash command from zsh shell), everything works as I want. I checked the version with bash…
paulvand
  • 31
  • 1
1
vote
2 answers

Does a double-asterisk wildcard mean anything apart from `globstar`?

I have an Ant build.xml script that includes the following snippet: According to the answers to this question and the Bash documentation, the double-asterisk…
JonahHuron
  • 297
  • 1
  • 2
  • 12
0
votes
1 answer

How bash inverse pattern matching works internally in extglob?

Below files are present in current directory. -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_july_2001_001.jpeg -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50 london_march_2004_002.png -rw-r--r-- 1 kazama kazama 0 Feb 16 08:50…
Sking
  • 3
  • 3
0
votes
1 answer

in bash how to put extglobed file names into array?

I knew I can do arr=(*.log) to get all *.log files into arr. But when I try extglob with more complex pattern it seems fail: $ shopt -s nullglob extglob; x=([a-z][0-9].+([0-9]).*.gz); echo "${x}"; shopt -u nullglob extglob; -bash: syntax error near…
Wang
  • 7,250
  • 4
  • 35
  • 66
0
votes
1 answer

Negative pattern match for lftp remove

In a GitHub action, I'd like to remove the old bundle files from the FTP server after deploying the new bundle. To achieve this, I thought to Deploy new bundle Parse the hashes of (main-\*.js, polyfills-\*.js etc) Remove all matching files except…
astriffe
  • 172
  • 14
0
votes
1 answer

Bash extended globbing bug?

There are three files in a directory: ab2 ab23 ab3 When I execute: ls ab+(2|3) It displays: ab2 ab23 ab3 instead of ab2 and ab3 only. Any ideas why it is like that? Is it a bug?
szawel
  • 3
  • 1
1
2