SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    unix help – efficiently finding files that contain a specific string
Go
New
Find
Notify
Tools
Reply
  
unix help – efficiently finding files that contain a specific string Login/Join 
Baroque Bloke
Picture of Pipe Smoker
posted
I want to efficiently find all paths whose leaf filenames are ‘*.sbl’ that are present in and below a specified directory AND contain a specific simple string such as ‘table’.

I’ve tried many combinations of ‘find’, ‘xargs’, and ‘grep’ without success.

I suspect that it’s a common task, but my web searches haven’t succeeded in finding useful help. The command ought to look roughly like this:

% find Progs/ -name *.sbl | xargs some_fancy_stuff_using_grep

My platform is bash on macOS. Thanks in advance!



Serious about crackers
 
Posts: 9729 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Optimistic Cynic
Picture of architect
posted Hide Post
The command line utility locate(1) is what you want. You can also do this with Spotlight in the GUI. I don't know if locate is in the default install, but if not, there are a few options for installing non-Apple command line tools. I can look them up if Google fails. From memory, one is MacPorts (but my memory isn't what it used to be).
 
Posts: 6978 | Location: NoVA | Registered: July 22, 2009Reply With QuoteReport This Post
Member
Picture of DrDan
posted Hide Post
On my Ubuntu machine I used

% find . -name *.cpp -exec grep -H include '{}' ';'

to produce a list of all files with extension ".cpp" that had an include file.

Piping find into grep takes the list of files found to search with grep, using -exec grep is executed on each result found with find.

ETA: the -H option to grep returns the entire path of the file that matches the search. For your stated problem, it would look close to

% find Progs/ -name *.sbl -exec grep -H what_ever_you_want_to_search_for '{}' ';'




This space intentionally left blank.
 
Posts: 5066 | Location: Florida | Registered: August 16, 2009Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
^^^^^^^^
Woo hoo – that works! I thank you DrDan.

I thank you too, architect. I do have:
/usr/bin/locate
On my MacBook, but I’m not currently familiar with that tool.

SIGforum is awesome!



Serious about crackers
 
Posts: 9729 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Member
posted Hide Post
find . -name '*.cpp'|xargs grep "some fancy stuff"
 
Posts: 1825 | Location: Austin TX | Registered: October 30, 2003Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
^^^^^^^
I’ll try that tomorrow, saigonsmuggler. Thank you.



Serious about crackers
 
Posts: 9729 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Member
Picture of wrightd
posted Hide Post
if you have the "tree" command in your kernel, check out "tree -d", "tree -f" command/switches. something along the lines of "tree -[d|f] |grep -i <your search substring whatever>

But your find command is pretty powerful and more flexible. one may be faster than the other though it might not be enough to make a difference. if you get lots of crap included in the result you can always pipe the stderr to null, as in ... 2>/dev/null, but it doesn't always work because of the order of precedence of stdout vs stderr in between the various commands and arguments (which I never mastered).




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9159 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
My report on the methods that DrDan and saigonsmuggler graciously crafted for me.

Both methods quickly find and report paths to specified files that contain the target string (the primary requirement). And both report the entire line that contained the target string. Not my requirement, but that’s a nice to have feature.

Both methods report the path to simlinks that have the target string, but don’t display the containing line. But they state: “No such file or directory”. Not a problem; the path is the only hard requirement. There may be switches to fix that tiny issue.

In summary, DrDan and saigonsmuggler went out of their way to help me out. I’m grateful!

ETA – I need this kind of operation often. I’ll write a bash script with one or two arguments for ease of use. I’ll put the script in my ~/.bashrc file.



Serious about crackers
 
Posts: 9729 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Member
Picture of DrDan
posted Hide Post
quote:
Originally posted by Pipe Smoker:

In summary, DrDan and saigonsmuggler went out of their way to help me out. I’m grateful!


Glad I could be of help.




This space intentionally left blank.
 
Posts: 5066 | Location: Florida | Registered: August 16, 2009Reply With QuoteReport This Post
  Powered by Social Strata  
 

SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    unix help – efficiently finding files that contain a specific string

© SIGforum 2024