DEV Community

Mike Whitaker
Mike Whitaker

Posted on

Shell command options you didn't know you needed #8

Another grep option!

This also tracks back to #6 and #7 in this series, and to be honest, it's more a shell option I really should have known existed. (I’m not proud: some of the reasoning behind these articles is so that you can learn from my “oh, duh” moments.)

As you may recall, we're tracking down a bunch of files with iso-8859-1 characters to convert to utf-8. Things weren't quite as I painted them in the previous posts, as I discovered the directory tree contained a bunch of binary files that I really didn't want to convert. Time to break out the brutal axe and chainsaw combo beloved of shell hackers everywhere, find and xargs.

find mydir -name '*.html' | xargs grep -l -avx '.*'

Upside, it works.
Massive downside for big directories (mine's probably several million files in a four or five deep tree), that's one process for every smallish group (see xargs --show-limits) of files. And it's freakin' SLOW (literally hours for me).

You may imagine me kicking myself (hey, I'm big enough to admit when I mess up!) when I was checking the man page for grep and found this FAR more elegant solution.

grep -l -r --include '*.html' -avx '.*' mydir

--include '<pattern>' makes grep only check files that match <pattern>. As with find -name, stick the pattern in quotes so the shell doesn't try and match it first. And yes, there's a --exclude as well :D.

Top comments (0)