A note about find and xargs

You can find lots of advice online, and in some Bash books, about using a program called xargs to accomplish something similar to the -exec action of find:

$ find vim -type f -name '*.vim' | xargs grep -F search --

Using -exec for this sort of situation instead should be possible one way or another with some careful programming, and it's safer and more portable too, because the only safe way to use xargs is with the non-standard find -print0 and xargs -0 options that specify null-byte terminators between each file, to safely and correctly deal with filenames that contain newlines:

$ find vim -type f -name '*.vim' -print0 | xargs -0 grep -F search --
Yes, file names on Unix can contain newlines, not just spaces. ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.