Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to limit the number of results returned by the FIND command on an unix system? We are having performance issue due to an unsually large number of files in some directories.

I'm tryging to do something like:

find /some/log -type f -name *.log -exec rm {} ; | limit 5000
share|improve this question

3 Answers 3

up vote 4 down vote accepted

It sounds like you're looking for xargs, but don't know it yet.

find /some/log/dir -type f -name "*.log" | xargs rm
share|improve this answer
-exec rm {} + would do the same thing without the overhead. though you could add head to the pipe chain: find [...] | head -5000 | xargs rm – quack quixote Feb 22 '10 at 21:57
I learned something new today. Thanks. – blahdiblah Feb 22 '10 at 22:04
Note that just find ...|xargs is dangerous, as it will do funny/weird/disastrous things if some file name contains funny characters. Always use find ... -print0 | xargs -0 (GNU extension, I believe). – sleske Feb 22 '10 at 22:58
Instead of -exec rm... or xargs rm you could use find's -delete flag. – Martin Hilton Feb 22 '10 at 23:19

You could try something like find [...] |head -[NUMBER]. This will send a SIGPIPE to find when head outputs its however-many lines so that find doesn't continue its search.

share|improve this answer
I should had add that i'm also using the -exec arg. The HEAD works if the command is something like LS. But it does not work in my case since i'm using RM and thats seems to take all the files in one execution. find /some/log -type f -name *.log -exec rm {} ; | HEAD -5000 – lemotdit Feb 22 '10 at 21:11
Instead of using -exec rm, just pipe the results of find to head as suggested, and then pipe the result to xargs and rm. – Paul R Feb 22 '10 at 21:53
find /some/log -type f -name *.log -exec rm {} ; | limit 5000

Well, the command as quoted will not work, of course (limit isn't even a valid command).

But if you run something similar to the find command above, it's probably a classic problem. You're probably having performance problems because find runs rm once for every file.

You want to use xargs, it can combine several files into one command line, so it will invoke rm a limited times for many files at once, which is much faster.

share|improve this answer
"limit" is not a valid command, and your ; is not properly escaped. This will not work. – amphetamachine Feb 24 '10 at 6:08
@amphetamachine: I just quoted the question. But you're right, of course. – sleske Feb 24 '10 at 10:25

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.