Skip to main content
added 122 characters in body
Source Link
phk
  • 6.1k
  • 7
  • 44
  • 76

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( -type f )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

I left out the command line parsing for -excl and such since you didn't make clear how your script's command line options generally look like, if it's truly just -excl then you can simply check for it ([[ "$1" = -excl ]]) and then shift.

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( -type f )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( -type f )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

I left out the command line parsing for -excl and such since you didn't make clear how your script's command line options generally look like, if it's truly just -excl then you can simply check for it ([[ "$1" = -excl ]]) and then shift.

added 122 characters in body
Source Link
phk
  • 6.1k
  • 7
  • 44
  • 76

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( '-type'type 'f'f )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( '-type' 'f' )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( -type f )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy
added 122 characters in body
Source Link
phk
  • 6.1k
  • 7
  • 44
  • 76

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( '-type' 'f' )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( '-type' 'f' )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing.

Solution using bash arrays:

#!/bin/bash

declare -a find_arguments=( '-type' 'f' )

for arg; do

  find_arguments+=( ! -name "$arg" )
 
done

find . "${find_arguments[@]}"

If you really want regex then change -name into -regex but from your example you seem to want globbing. (BTW, -regex is not POSIX but supported by GNU find.)

Demo

touch {a,b,c}{x,y,z}
./myscript.sh 'a*' '*z'

Output:

./bx
./by
./cx
./cy
Source Link
phk
  • 6.1k
  • 7
  • 44
  • 76
Loading