Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Is there any way to tell ack to only search for text on the current folder? (or specify a max-depth level?) And with grep?

share|improve this question
No, ack does not have a max-depth option, other than the -n to not recurse at all. – Andy Lester Apr 24 '14 at 14:41

2 Answers 2

up vote 7 down vote accepted

Use -n for no-recurse:

$ ack -n foo

grep is not recursive by default, and you should use the -r flag only if you want a recursive search.

You can search the current directory with grep as follows:

$ grep -- foo *
share|improve this answer
Doesn't answer the question? (what if I want to recurse to a depth of 2) – Steven Lu Aug 25 at 17:13
Good point, I missed that. Any suggestions? – Eric Wilson Aug 25 at 21:33

You can couple find with the -exec argument. example:

find . -maxdepth 1 -exec grep foo {} \;

This can be scaled, i.e. -maxdepth 2

share|improve this answer
Stephane, my answer (-maxdepth 1) was scaleable. For example -maxdepth 2. I do not know how to describe the edit you made as such. – David Wilkins Jan 30 '14 at 20:15
it will not reach @stephane if you don't use the @. – Braiam Jan 31 '14 at 5:26
Yes sorry, I agree my edit was too much intrusive. You may still want to clarify that that (-maxdepth) is not portable/standard syntax (only GNU and some BSDs). Also, there's no point using \; here (run one grep per file). Use grep -H foo {} + (GNU specific) or grep foo /dev/null {} + (standard) to make sure the file name is always printed. The standard equivalent to -maxdepth 2 would be find . -path './*/*' -type d -prune -o -type f -exec ... – Stéphane Chazelas Jan 31 '14 at 6:57
Also note that yours will give error messages for directories (including . as you don't give the -mindepth 1) while GNU grep will not try to read directories with -r (it recurses on them). You may want to add a -d skip to grep (assuming GNU grep) or better add ! -type d to find or even better -type f (or -xtype f assuming GNU find) as you probably don't want grep to read non-regular files. – Stéphane Chazelas Jan 31 '14 at 7:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.