Is there a way to force the
find command to stop right after finding the first match?
With GNU find, you can use the
If all you do is printing the name, and assuming the filenames don't contain newline characters, you could do:
That will not stop
If you're doing more than printing the paths of the found files, you could try this approach:
That has the side effect of
Actually, using the SIGPIPE signal instead of SIGTERM (
find . -name something -print -quit Terminates find after the first match after printing it.
Terminate find after a specific amount of matches and print results: find . -name something -print | head -n 5 Surprisingly enough - head now terminates string after 5 matches, though I do not know how or why.
It is very easy to test: Just let find search a on root which would result thousands, maybe even more matches while taking at least a minute or more. But when piped into "head" "find" will terminate after the specified amount of lines defined in head (default head shows 10, use "head -n" to specify lines).
Note that this will terminate after "head -n" reaches the specified newline character count and therefore any match that contains multiple newline characters will count accordingly.
For entertainment purposes, here's a lazy find generator in Bash. This example generates a ring over the files in the current directory. Read however many you want then