Skip to content
shellhacks.dev
Text Processing

Search only certain file types

Search only certain file types with grep -r --include='*.sh'. When --exclude comes first, files that match neither option are searched as well.

1 min read
Summarize in AI

A recursive search should look at shell scripts only, and adding an exclusion makes text files appear in the results.

Shell
mkdir -p proj/sub
echo 'TODO shell' > proj/a.sh
echo 'TODO text' > proj/b.txt
echo 'TODO deep' > proj/sub/c.sh
cd proj
grep -r --include='*.sh' TODO .
echo ---
grep -r --exclude='c*' --include='*.sh' TODO .
Text
./sub/c.sh:TODO deep
./a.sh:TODO shell
---
./b.txt:TODO text
./sub/c.sh:TODO deep
./a.sh:TODO shell

--include takes a glob that grep matches against each base name while it walks the tree, so the first command finds both scripts and skips b.txt. The GNU grep manual gives the rule for mixing the two options: the last one that matches a file wins, and a file that matches none is searched unless the first such option was --include. In the second command the first option is --exclude, so b.txt is searched, and c.sh is searched too because the later include overrides the exclusion.

The glob is matched against the base name, never a path with a slash in it. Quote it, so the shell leaves it for grep. Put every --include first when the intent is to search only those files. Directories are not filtered by these options, which is what --exclude-dir is for.

Share

More snippets

Text Processing

Search recursively and skip binary files

Skip binary files in a recursive grep with --binary-files=without-match, short form below. Otherwise GNU grep 3.8 prints a notice for every binary hit.

1 min read
Text Processing

Count the matches per file

Count the matching lines in each file with grep -c. With several files grep prefixes each count with the name, and -h removes the prefix for one total.

1 min read
Text Processing

Count every occurrence not just lines

Count every occurrence of a word with grep -o piped to wc -l, because grep -c counts the lines that match and a line with three hits counts as one.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.