Skip to main content
added 69 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#! /bin/bash -
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!
 
joinlist() {
IFS='|' # local"$*" IFS='|'
joins with echothe "$*"first character of IFS
pattern="**/*@(${*})*"

pattern=$(joinlistIFS= $*)# do globbing but not splitting upon unquoted expansion:
matches=( **/*@(${pattern})*$pattern ) 

for element in $matches;"${matches[@]}"; do
  printf '%s\n' "${element}"
done

If you want to have it as a function, just put pattern=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!
 
joinlist() {
  local IFS='|'
  echo "$*"   
}

pattern=$(joinlist $*)
matches=( **/*@(${pattern})* )
for element in $matches; do
  printf '%s\n' "${element}"
done

If you want to have it as a function, just put pattern=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#! /bin/bash -
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

IFS='|' # "$*" joins with the first character of IFS
pattern="**/*@(${*})*"

IFS= # do globbing but not splitting upon unquoted expansion:
matches=( $pattern ) 

for element in "${matches[@]}"; do
  printf '%s\n' "${element}"
done

If you want to have it as a function, just put pattern=… to done in a function declaration.

added 8 characters in body
Source Link
Marcus Müller
  • 52k
  • 4
  • 80
  • 123
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*+*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

joinlist() {
  local IFS='|'
  echo "$*"   
}

matcher=$pattern=$(joinlist $*)
matches=( **/*+*@(${matcherpattern})* )
for matchelement in matches;$matches; do
  printf '%s\n' $match"${element}"
done

If you want to have it as a function, just put matcher=…pattern=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*+(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

joinlist() {
  local IFS='|'
  echo "$*"   
}

matcher=$(joinlist $*)
matches=( **/*+(${matcher})* )
for match in matches; do
  printf '%s\n' $match
done

If you want to have it as a function, just put matcher=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*@(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

joinlist() {
  local IFS='|'
  echo "$*"   
}

pattern=$(joinlist $*)
matches=( **/*@(${pattern})* )
for element in $matches; do
  printf '%s\n' "${element}"
done

If you want to have it as a function, just put pattern=… to done in a function declaration.

added 82 characters in body
Source Link
Marcus Müller
  • 52k
  • 4
  • 80
  • 123
$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*+(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

joinlist() {
  local IFS='|'
  echo "$*"   
}

matcher=$(joinlist $*)
matches=( **/*+(${matcher})* )
for match in matches; do
  printf '%s\n' $match
done

If you want to have it as a function, just put matcher=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
echo **/*+(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches

joinlist() {
  local IFS='|'
  echo "$*"   
}

matcher=$(joinlist $*)
matches=( **/*+(${matcher})* )
for match in matches; do
  printf '%s\n' $match
done

If you want to have it as a function, just put matcher=… to done in a function declaration.

$ find -iname '*foo*' -o -iname '*bar*' -o -iname '*blah*'

that's one way to go about it, but frankly, it is result-wise similar to

shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nocasematch  ## take a guess!

echo **/*+(foo|bar|blah)*

(but it does that without help of find).

We can very quickly build a shell script from that.

#!/bin/bash
shopt -s globstar  ## enable recursive globbing operator **
shopt -s extglob   ## enable (|) pattern lists
shopt -s nullglob  ## don't error if nothing matches
shopt -s nocasematch  ## take a guess!

joinlist() {
  local IFS='|'
  echo "$*"   
}

matcher=$(joinlist $*)
matches=( **/*+(${matcher})* )
for match in matches; do
  printf '%s\n' $match
done

If you want to have it as a function, just put matcher=… to done in a function declaration.

Source Link
Marcus Müller
  • 52k
  • 4
  • 80
  • 123
Loading