1832279 Members
2265 Online
110041 Solutions
New Discussion

find files

 
ust3
Regular Advisor

find files

I have a directory , there are hundreds of files in it , I want to find the files that are contains the string "abc" or "def" , and not contains the string "ghi" or "jkl" , can advise how to do ? thx
8 REPLIES 8
Kapil Jha
Honored Contributor

Re: find files

ls -lrt|egrep "abc|def"|egrep -v "ghi|jkl"

Hope this help.
BR,
Kapil
I am in this small bowl, I wane see the real world......
Hemmetter
Esteemed Contributor

Re: find files

hi,

try:
$ cd /your/dir
# # first find those having abc or def
$ find . -type f -exec grep -l -E "abc|def" {} \+ > files-with-abc-def

# # now throw away those having ghi or jkl:

$ cat files-with-abc-def | xargs grep -l -E -v "ghi|jkl"


rgds
HGH
Hemmetter
Esteemed Contributor

Re: find files

Hi

Kapils solution finds files with names matching your patterns:
you can use even simpler:
$ ls *abc* *def* | grep -E -v "ghi|jkl"


My solution works on file DATA, i.e. lists names of files (not) containing your patterns.

rgds
HGH
ust3
Regular Advisor

Re: find files

Thx reply,

The below script is fine.

ls -lrt|egrep "abc|def" * |egrep -v "ghi|jkl"

I want to ask again, if the "abc" that I want to find is a word not string , that mean there should be space behind and ahead the word "abc" , can advise what can i do ? thx


Hemmetter
Esteemed Contributor

Re: find files

Hi

use grep with parameter -w


from grep(1) manpage:

-w
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

rgds
HGH
Kapil Jha
Honored Contributor

Re: find files

>>>if the "abc" that I want to find is a word not string

What you mean by word...I think word itself is a string.
And if you grep in double coats it would work I suppose.
BR,
Kapil
I am in this small bowl, I wane see the real world......
Sandman!
Honored Contributor

Re: find files

>I want to ask again, if the "abc" that I want to find is a word not string , that >mean there should be space behind and ahead the word "abc" , can advise >what can i do ? thx

# ls -lrt|egrep " (abc|def) " * |egrep -v " (ghi|jkl) "
ust3
Regular Advisor

Re: find files

Thx all