Linux : Search for a Particular word in a List of files under a directory
grep
is made for this.
Use:
grep myword *
for a simple wordgrep 'my sentence' *
for a literal stringgrep "I am ${USER}" *
when you need variable replacement
You can also use regular expressions.
Add -r
for recursive and -n
to show the line number of matching lines.
And check man grep
.
linux + find word in file under directory but quickly
grep -r "string"
The find is not neccesary.
This is a good link, though outdated.
link text
Also i think this belongs in superuser.com
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 dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Fast search for text in files in a directory in unix?
This might be overkill for your purposes, but Beagle allows you to perform very fast searches of local files. It's usually marketed as a desktop application, but in fact it is just a daemon that can respond to requests from the command-line using beagle-query.
How can I use grep to find a word inside a folder?
grep -nr 'yourString*' .
The dot at the end searches the current directory. Meaning for each parameter:
-n Show relative line number in the file
'yourString*' String for search, followed by a wildcard character
-r Recursively search subdirectories listed
. Directory for search (current directory)
grep -nr 'MobileAppSer*' .
(Would find MobileAppServlet.java
or MobileAppServlet.class
or MobileAppServlet.txt
; 'MobileAppASer*.*'
is another way to do the same thing.)
To check more parameters use man grep command.
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.
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.
Related Topics
Profiling Arbitrary Cuda Applications
Goroutine in Io Wait State for Long Time
Show Image Notification from Bash Script
Git Clone Using Ssh Failed in Windows Due to Permission Issue
Environment Variables in Symbolic Links
Why Does 'Ping' Not Timeout in Linux
Setting The Umask of The Jenkins Process
Get The Count of Bytes Waiting on a Serial Port Before Reading, Linux
Bash Cut Columns to One File and Save onto The End of Another File
Disable CPU Caches (L1/L2) on Armv8-A Linux
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)
Stty: Standard Input: Inappropriate Ioctl for Device
Linux Telnet Vt100 Return Key Sends ^M
Copying Files with Wildcard (*) to a Folder in a Bash Script - Why Isn't It Working