Files and Pipelines
Find and Grep
Searching a Project
Bash often coordinates search tools. find locates candidate files, and grep checks their contents.
Program
Play the script to see a project search narrow from package.json directories to a file containing login.
find_grep.sh
#!/usr/bin/env bash
root="$(mktemp -d)"
mkdir -p "$root/app" "$root/docs"
printf '{"scripts":{"test":"echo ok"}}\n' > "$root/app/package.json"
printf 'login screen\n' > "$root/app/login.txt"
match="$(find "$root" -name package.json -exec dirname {} \; | while read -r dir; do grep -l "login" "$dir"/* 2>/dev/null | sed "s|$root/||"; done)"
rm -rf "$root"
echo "$match"
find
`find` walks directories and selects paths by name, type, and other tests.
grep
`grep` searches file contents for matching text.