Go | New | Find | Notify | Tools | Reply |
Baroque Bloke |
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 | ||
|
Optimistic Cynic |
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). | |||
|
Member |
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. | |||
|
Baroque Bloke |
^^^^^^^^ 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 | |||
|
Member |
find . -name '*.cpp'|xargs grep "some fancy stuff" | |||
|
Baroque Bloke |
^^^^^^^ I’ll try that tomorrow, saigonsmuggler. Thank you. Serious about crackers | |||
|
Member |
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 | |||
|
Baroque Bloke |
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 | |||
|
Member |
Glad I could be of help. This space intentionally left blank. | |||
|
Powered by Social Strata |
Please Wait. Your request is being processed... |