Breadth-first search with ack or grep

11

1

When I search e.g. my home directory with ack (or grep), I normally want to know where I set a specific option.

Since most config files are really close to ~ it would considerably speed up ack if I could search breadth-first. Is this possible?

Profpatsch

Posted 2014-12-06T14:30:24.127

Reputation: 417

1Sadly the answer seems to be no. – Nifle – 2014-12-08T13:27:22.037

4It seems every couple of months I google this and get back to this page. – Gregory Nisbet – 2016-06-15T18:32:13.043

2@GregoryNisbet My „solution“ has been to switch to ag, which is about 5–10 times faster than ack and provides no drawbacks. Combined with switching to SSD I haven’t had the problem since. – Profpatsch – 2016-06-21T19:54:08.977

Answers

3

I don't know ack but with tools such as grep I typically use:

( find . -maxdepth 1 -print ; find . -mindepth 2 -print ) | xargs -n 50 -exec grep TXT

The part between ( and ) ensures that first the files at level 1 are listed and after that those at 2 and deeper (you can vary). xargs feeds the file names per 50 to grep.

Of course it depends on which variant of find is available on your platform. If running something from 30 years ago, you will need to use something like sorting on the number of forward slashes.

Guido Leenders

Posted 2014-12-06T14:30:24.127

Reputation: 746

MMV (Archlinux): grep: ./.macromedia/Flash_Player/#SharedObjects/Q2GSRK6Y/www.mixcloud.com/media: Is a directory – xtian – 2017-08-04T17:08:31.060

If you're not going to use -print0 to find, at least do find . -maxdepth 1 -exec grep {} +;. https://mywiki.wooledge.org/BashFAQ/020

– Limited Atonement – 2018-04-10T19:44:37.310