Grep Search all files in directory for string1 AND string2
grep -r db-connect.php . | grep version
grep Command for Files with String1 AND NOT String2
You can use the -L
flag:
-L, --files-without-match print only names of FILEs containing no match
First you find the files that doesn't contain "String2", then you run on those and find the ones that does contain "String1":
$ grep -L "String2" * | xargs grep -nH "String1"
Recursively locate all files that have string a AND string b using grep
Pipe the results of your first search to grep
again:
grep -RlZ "String1" . | xargs -0 grep -l "String2"
This would list the files containing both String1
and String2
.
Getting the line numbers for the files containing both the strings wouldn't be probably very efficient since you need to know that a priori. One way would be to again pipe the results to grep
:
grep -RlZ "String1" . | xargs -0 grep -lZ "String2" | xargs -0 grep -En 'String1|String2'
Find all files containing two patterns
This will do, even when the strings are not on the same line:
find ~ -type f -name \*.txt -exec grep -iq string1 \{\} \; -exec grep -iq string2 \{\} \; -print
Find files containing multiple strings
you can also try this;
find . -type f -exec grep -l 'string1' {} \; | xargs grep -l 'string2'
this shows file names that contain string1 and string2
Finding multiple strings in directory using linux commends
I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.
grep -rn "text_to_search" directory_name/
example:
$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string
output would be:
menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);
Regex/Grep to filter strings in text file
You can use a single pattern matching either word or excel or an empty line using an alternation and check if there is a reversed result, and use --quiet
to not write to standard output.
if grep --quiet -v 'word\|excel\|^[[:space:]]*$' document.txt; then
echo "error"
fi
Related Topics
How to Boot the Linux Kernel Without Creating an Initrd Image
Mmap: Will the Mapped File Be Loaded into Memory Immediately
What Is File Hole and How Can It Be Used
Why Should I Recompile an Entire Program Just for a Library Update
Packaging Proprietary Software for Linux
How to Stop 'Uninterruptible' Process on Linux
How to Change Port Number for Jenkins Installation in Ubuntu 12.04
How to Pipe Output to a File When Running as a Systemd Service
Find Directories Having Size Greater Than X Mb
How to Diff Top Lines of Two Files Without Intermediate File
Calculate Total Used Disk Space by Files Older Than 180 Days Using Find
Change Filenames to Lowercase in Ubuntu in All Subdirectories
Bash: Get List of Commands Starting with a Given String
200,000 Images in Single Folder in Linux, Performance Issue or Not
Why Can't We Use C Standard Library Functions in Kernel Development
The Address Where Filename Has Been Loaded Is Missing [Gdb]
Can Malloc_Trim() Release Memory from the Middle of the Heap