Unix Command to List files containing string but *NOT* containing another string
Try this:
grep -rl <string-to-match> | xargs grep -L <string-not-to-match>
Explanation: grep -lr
makes grep recursively (r) output a list (l) of all files that contain <string-to-match>
. xargs loops over these files, calling grep -L
on each one of them. grep -L
will only output the filename when the file does not contain <string-not-to-match>
.
How do I find files that do not contain a given string pattern?
The following command gives me all the files that do not contain the pattern foo
:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep 0
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
.
What is a Linux command to find files containing some strings but not another?
There's more than one way to do it. This should keep the total number of processes executed to a minimum:
find . -name \*.xml -print0 \ # List of *.xml files (NUL-terminated)
| xargs -0 grep -Zli 'string[12]' \ # is input to first grep, which sends (NUL-term'd)
| xargs -0 grep -Li 'string[34]' # file list to second grep
Thanks Matt Gibson for reminding us of the -Z
flag to GNU grep.
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.
How to find text files not containing text on Linux?
The command you quote, ironically enough does exactly what you describe.
Test it!
echo "hello" > a
echo "bye" > b
grep -iL BYE a b
Says a only.
I think you may be confusing -L and -l
find . -print | xargs grep -iL "somestring"
is the inverse of
find . -print | xargs grep -il "somestring"
By the way, consider
find . -print0 | xargs -0 grep -iL "somestring"
Or even
grep -IRiL "somestring" .
Find files not containing search string
You could achieve this by something like
grep -L keyword $( find /start/path -type f )
You may need GNU grep
for -L
.
Related Topics
Check If Environment Variable Is Already Set
Given Two Directory Trees How to Find Which Files Are the Same
How to Register Fuse Filesystem Type with Mount(8) and Fstab
How to Use Linux Command Sort to Sort the Text File According to 4Th Column, Numeric Order
Shared Libraries: Windows VS Linux Method
Why Does Perf Stat Show "Stalled-Cycles-Backend" as <Not Supported>
How to Create a Waveform Image of an Mp3 in Linux
Run a Command at a Specific Time
Wkhtmltopdf Installation Error on Ubuntu
Tcp: When Is Epollhup Generated
Does Epoll(), Do Its Job in O(1)
"Docker Images" Shows Image, "Docker Rmi" Says "No Such Image" or "Reference Does Not Exist"
Scp Command Syntax for Copying a Folder from Local MAChine to a Remote Server
How to Write Non-Ascii Characters Using Echo
"Git Add" Returning "Fatal: Outside Repository" Error
How to Add a Custom Footer to Sphinx Documentation? (Restructuredtext)
Linux Command to Check New Files in File System
Groovy Process Not Working with Linux Shell (Grep and Awk and Ps)