DEV Community

abbazs
abbazs

Posted on • Edited on

4 3

How to find files containing a specific text using grep?

Using grep we can do this.

grep -rnw '/path/to/somewhere/' -e 'pattern'
Enter fullscreen mode Exit fullscreen mode

-r or -R is recursive
-n is line number
-w stands for match the whole word
-l (lower-case L) can be added to just give the file name of matching files
-e is the pattern used during the search
--exclude, --include, --exclude-dir flags can be used for targeted search

To search python or in json files only

grep --include=\*.{py,json} -rnw '/path/to/somewhere/' -e "pattern"
Enter fullscreen mode Exit fullscreen mode

To search excluding pyc files

grep --exclude=\*.pyc -rnw '/path/to/somewhere/' -e "pattern"
Enter fullscreen mode Exit fullscreen mode

To search excluding directories

grep --exclude-dir={test,.vscode, *__cache*} -rnw '/path/to/somewhere/' -e "pattern"
Enter fullscreen mode Exit fullscreen mode

To list only the file names containing search text

grep -rl '/path/to/somewhere/' -e "pattern"
Enter fullscreen mode Exit fullscreen mode

For detailed options refer to man pages or man grep

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay