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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay