'findgrep' uses find
to recursively search for all filenames in
a path that match -name
, and then uses 'grep' to search for a
specified string within those files. If the string is found, the
filename and 'grep' search output are returned.
'findgrep' can be executed directly as a script, or sourced into the current shell.
findgrep [find_path [grep_search]] [-options] [-grep_opts ...]
find_path: Path to recursively search for files. Defaults to current directory (/ai/scripts/findgrep).
grep_search:
The string to search for within the files. Defaults to an empty
string (meaning grep
is not performed on files).
grep_opts:
Additional options to pass to the grep
command.
-n|-name|--name find_name
Where `find_name` is the argument for the `find -name` option
in `find`, such as '*.bash' or '*.py', except for the
keywords 'bash', 'php', 'python', or 'shell'.
(See "Special Find Names" below.)
-C|--usecolor
Force use of ansi colour.
+C|+usecolor
Never use ansi color
-l|--files-with-matches
Only display file names, with no ansi color.
-m|-maxdepth|--maxdepth depth
Where `depth` is the argument for the `find -maxdepth` option.
Default is unlimited.
-v|--verbose
Be verbose
-q|--quiet
Be not verbose
-V|--version
Print version: 'bash_docstring 0.4.20'
-h|--help
This `bash_docstring`.(Not available if this script is
`source`d as a function.)
findgrep ~/lib -name '*.bash' 'openai' -i
findgrep ~/scripts --name py '__main__' -m1
findgrep . '__main__' -name '*' -m1
findgrep . -n shell 'trim ' >/tmp/funcs_using_trim
findgrep /ai/scripts/lib -n shell 'trim ' -ql
Bash >= 5
The script exits if any command returns a non-zero status, if any variable is us before being set, or if any part of a pipeline fails.
If 'find_name' equals 'bash', 'php', 'python', or 'shell', then 'findgrep' will search all text files regardless of extension for the presense of an appropriate hashbang (#!/() or <?()) for a range of file extensions related to those types of files.
Note that use of these keyword names will incur additional processing overhead.
bash: matches files with file extension .bash|.sh, or files with hashbangs containing /bash|/sh.
shell: matches files with file extension .bash|.sh|.zsh, or files with hashbangs containing /bash|/sh|/zsh.
python: matches files with file extension .py|.python, or files with hashbangs containing /python.
php: matches files with file extension .php, or files with hashbangs containing /php, or files with a header starting with '<?php'.