> esc
move open g h home ? help
$ cat /post/find-in-files-script.md

Find in files script

Similar to the previous post, I can never remember the correct format for grepping recursively through folders for a particular string. So I added the following script to a file called findinfiles in the scripts directory detailed in the previous post:

#!/bin/bashif [[ "$2" != "" ]]; then    grep -inrH --include="$2" "$1" .else    grep -inrH "$1" .fi

Variables are quoted so strings containing spaces or shell metacharacters pass through cleanly. Don’t forget to mark the script executable:

chmod +x findinfilesfindinfiles mystringfindinfiles mystring '*.html'

The if checks for a second parameter and, if present, passes it to --include= to restrict the recursive search to matching files. Without it the search covers everything. The script doesn’t follow symlinks; add -R (capital R) to grep if you want it to.

Modern alternative: rg (ripgrep) is a drop-in faster grep -r that respects .gitignore by default. It’s in most distros (sudo apt install ripgrep on Debian/Ubuntu; brew install ripgrep on macOS). For everyday in-files search I reach for it before grep.

$ env | grep ^CAT
CAT_01=scripting
CAT_02=sysadmin
$ env | grep ^TAG
TAG_01=bash
TAG_02=grep