Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Should we assume it was \; not just ; ? – Putnik Jan 11 at 21:01
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

Just |head didn't work for me:

root@static2 [/home/dir]# find . -uid 501 -exec ls -l {} \; | head 2>/dev/null
total 620
-rw-r--r--  1 root   root           55 Sep  8 15:22 08E7384AE2.txt
drwxr-xr-x  3 lamav statlus 4096 Apr 22  2015 1701A_new_email
drwxr-xr-x  3 lamav statlus 4096 Apr 22  2015 1701B_new_email
drwxr-xr-x  3 lamav statlus 4096 May 11  2015 1701C_new_email
drwxr-xr-x  2 lamav statlus 4096 Sep 24 18:58 20150924_test
drwxr-xr-x  3 lamav statlus 4096 Jun  4  2013 23141_welcome_newsletter
drwxr-xr-x  3 lamav statlus 4096 Oct 31  2012 23861_welcome_email
drwxr-xr-x  3 lamav statlus 4096 Sep 19  2013 24176_welco
drwxr-xr-x  3 lamav statlus 4096 Jan 11  2013 24290_convel
find: `ls' terminated by signal 13
find: `ls' terminated by signal 13
find: `ls' terminated by signal 13
find: `ls' terminated by signal 13
find: `ls' terminated by signal 13


My (definitely not the best) solution:

find . -uid 501 -exec ls -l {} \; 2>/dev/null | head

The disadvantage is that the 'find' itself isn't terminated after required number of lines, and run in background until ^C or end, therefore ideas are welcomed.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .