Using the find command to search for all files that have some text pattern

I use the following find command to search and display all files that have an input text template.

to find. -type f -print | xargs grep -n "pattern"

I have many project folders, each of which has its own makefile called "Makefile". (without file extension, just β€œMakefile”)

How to use the command above to search for a specific template only in files with the name Makefile, which are present in all folders of my project?

-AD.

+4
source share
7 answers

-print not required (at least with GNU find ). The -name argument allows you to specify a file name pattern. Therefore, the command will be:

find . -name Makefile | xargs grep pattern

+6
source
 find . -type f -name 'Makefile' | xargs egrep -n "pattern" 

use egrep if you have a very long way

Duplicate: this

+1
source

If you have spaces or odd characters in your directories, you need to use the zero-termination method:

  find . -name Makefile -print0 | xargs -0 grep pattern 
+1
source

You can avoid using xargs with -exec :

 find . -type f -name 'Makefile' -exec egrep -Hn "pattern" {} \; 

-H on egrep to print the full path to the corresponding files.

+1
source

you can use ff ie ff -p.format command. For example, ff -p * .txt

0
source

Find large files occupying large disk spaces

we need to combine several teams.

 find . -type f | xargs du -sk | sort -n | tail; 
0
source

All Articles