Search directory and search File

Malarose03

New Member
Joined
Aug 23, 2020
Messages
4
Reaction score
0
Credits
43
How can i first search with certain filename with wild card and then search a string in those filename and get the output.

E.g
Directory name : abc/def/xyz/
Files in directory : Abc12, abc13, abd12,abc14,abc15, abe12 and so on.

First search : ab*12
Output : abc12,abd12,abe12

Now search string 'pull' in the output files from first search and display the output with filename and the line where string is present.

I tried with below but no luck :
Ls -lrt Grep 'ab.*12' | xargs grep pull

May be a single command or a series of command that i can put in a script and execute, anything will do as long as the output comes as desired.

Thanks in advance.
 


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
8,628
Reaction score
7,587
Credits
35,291
G'day folks and welcome @Malarose03 :)

Moving this to Command Line, where more responses may be generated.

Cheers

Chris Turner
wizardfromoz
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,704
Reaction score
2,471
Credits
13,677
Also, you can use find with grep like this:
Bash:
find ./ -type f -iname "ab*12" -exec grep -RiHn pull {} \;
That will find files with names of any length that start with ab and end in 12 and then invokes grep to search matching files for the string pull.
NOTE:
In the above, the options I've used with grep are:
-R - recursive
-i - case insensitive
-H - show file-name of matched line
-n - show line-number of matched line

If you want the grep search to exactly match "pull" and not "PULL", "pUll" or any other upper/lowercase combination, remove the -i option from the grep invocation. Then it becomes a case-sensitive search.
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com


Top