0

Mac OSX Bash Shell

I want to use find to identify anything (directories or files) which do not follow an input pattern.

This works fine:

find . -path /Users/Me/Library -prune -o \! \( -path '*.jpg' \)

However I want to have a general ability to do from a bash alias or function eg:

alias negate_find="find . -path /Users/Me/Library -prune -o \! \( -path ' "$1" ' \)"

To allow shell input of the search term (which may contain spaces). The current syntax does not work, returning unknown primary or operator on invocation. Grateful for assistance in what I am doing wrong.

Richard L
  • 99
  • 1
  • 6
  • Does this answer your question? [Make a Bash alias that takes a parameter?](https://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter) – Socowi Jul 31 '21 at 07:57

1 Answers1

0

Not entirely sure why, but separating the input parameter into its own string seemed to work. Here it is as a working shell function and case invariant.


negate_find () {
search="$1"
lowersearch=$(echo "$search" | awk '{print tolower($0)}')
uppersearch=$(echo "$search" | awk '{print toupper($0)}')
echo "search = $search"
find . -path $HOME/Library -prune -o \! \(  -path "$lowersearch"  \)  -a \! \(  -path "$uppersearch"  \)
}
export -f negate_find



Richard L
  • 99
  • 1
  • 6
  • This worked because you used a function instead of an alias. Aliases cannot use `$1`. That you used `search=$1` and then `$search` instead of `$1` is irrelevant. Btw: Most `find` implementations have the case-insensitive `-ipath` (macOS' has too). You might want to use this instead of `lowersearch` and `uppersearch`. – Socowi Jul 31 '21 at 07:57