Bash command to search for any occurrence of phrase and return list of files and paths
{ find . -name '*my key phrase*' ;
grep -rl 'my key phrase' . ;
} | sort -u > mylist.txt
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
.
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.
Find all files with a filename beginning with a specified string?
Use find
with a wildcard:
find . -name 'mystring*'
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 a string in multiple files and return the names of files in Powershell?
This should give the location of the files that contain your pattern:
Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path
Linux find file names with given string recursively
Use the find command,
find . -type f -name "*John*"
Related Topics
Kaldi Toolkit Installation Error on Ubuntu 16.04
Google Suggest Query Using Curl
Having Linux Persist Memory Changes to Disk
Testing - Intentionally Corrupt a .Z File Using 'Dd'
How to Configure/Make/Install Against an Older Version of a Library
Influxdb Not Asking for Authentication
Linux - Bash Redirect a String to a File
How to Switch Between Different Versions of Julia (Specifically Between V0.3 and V0.4 on Ubuntu)
Git Status Between Windows and Linux Does Not Agree
Is There Any Way for Ioctl() in Linux to Specify Submission Queue Id for a Nvme Io Request
Aosp Build Error: Unrecognized Module Type "Hidl_Package_Root"
How to Find Grid Points Nearest to Given Location Using Shell Script
Why Doesn't ''Var=Value Echo $Var'' Emit Value
Printing Floating Point Numbers in Assembler
The Behavior When a Gnu Make Phony Target Happens to Be The Same as a Directory Name