Take the 2-minute tour ×
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 at 14:41

2 Answers 2

up vote 5 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

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 at 20:15
    
it will not reach @stephane if you don't use the @. –  Braiam Jan 31 at 5:26
1  
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 at 6:57
1  
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 at 7:01

Your Answer

 
discard

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.