Find all files matching 'name' on linux system, and search with them for 'text'
find / -type f -name filename.ext -exec grep -l 'lookingfor' {} +
Using a +
to terminate the command is more efficient than \;
because find
sends a whole batch of files to grep
instead of sending them one by one. This avoids a fork/exec for each single file which is found.
A while ago I did some testing to compare the performance of xargs
vs {} +
vs {} \;
and I found that {} +
was faster. Here are some of my results:
time find . -name "*20090430*" -exec touch {} +
real 0m31.98s
user 0m0.06s
sys 0m0.49s
time find . -name "*20090430*" | xargs touch
real 1m8.81s
user 0m0.13s
sys 0m1.07s
time find . -name "*20090430*" -exec touch {} \;
real 1m42.53s
user 0m0.17s
sys 0m2.42s
How to find all files containing specific text (string) on Linux
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-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
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Find all files with name containing string
Use find
:
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1
if you want it recursive) containing "string" and will print it on the screen.
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
If you want to use grep
(but I think it's not necessary as far as you don't want to check file content) you can use:
ls | grep touch
But, I repeat, find
is a better and cleaner solution for your task.
How to find all files containing specific text (string) on Linux
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-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
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Linux find file names with given string recursively
Use the find command,
find . -type f -name "*John*"
Find all files with a filename beginning with a specified string?
Use find
with a wildcard:
find . -name 'mystring*'
How to search for a text in specific files in unix
It might be better to use find
, since grep
's include/exclude can get a bit confusing:
find -type f -name "*.xml" -exec grep -l 'hello' {} +
This looks for files whose name finishes with .xml
and performs a grep 'hello'
on them. With -l
(L) we make the file name to be printed, without the matched line.
Explanation
find -type f
this finds files in the given directory structure.-name "*.xml"
selects those files whose name finishes with.xml
.-exec
execute a command on every result of thefind
command.-exec grep -l 'hello' {} +
executegrep -l 'hello'
on the given file. With{} +
we are refering to the matched name (it is like doinggrep 'hello' file
but refering to the name of the file provided by thefind
command). Also,grep -l
(L) returns the file name, not the match itself.
Related Topics
How to Test If an Address Is Readable in Linux Userspace App
Why Do I Have to 'Wait()' for Child Processes
Bash: Read Stdin from File and Write Stdout to File
How to Read the Mouse Button State from /Dev/Input/Mice
How to Specify Non-Capturing Groups in Sed
Get Lines of File1 Which Are Not in File2
Switch/Case Doesn't Work in Awk
Why Are Core Dump Files Generated
Get Link Speed Programmatically
Vim Background with Gnu Screen
Jenkins Path to Git Windows Master/Linux Slave
Sorting Numbers with Multiple Decimals in Bash
How to Read Just a Single Character in Shell Script