Skip to content

Instantly share code, notes, and snippets.

@maxsu
Last active December 20, 2015 12:09
Show Gist options
  • Save maxsu/6128886 to your computer and use it in GitHub Desktop.
Save maxsu/6128886 to your computer and use it in GitHub Desktop.
Find Script
#!/bin/bash
# usage: myfind [-d DIR] STRING [...]
# finds all files matching any given STRING in DIR, or the current working
# directory if DIR is not provided
arg=.
args=.
dir=.
# if -d was used, store DIR and shift away -d DIR
if [[ $1 = -d ]]; then
dir=$2
shift 2
fi
# make sure at least one STRING was provided
if ((!$#)); then
cat <<'EOF' >&2
usage: myfind [-d DIR] STRING [...]
finds all files matching any given STRING in DIR, or the current working
directory if DIR is not provided
EOF
return 1
fi
# create list of arguments for find
args=(-iname "*$1*")
shift
for arg; do
args+=(-o -iname "*$arg*")
done
# run the actual find command
find "$dir" \( "${args[@]}" \)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment