Using find with xargs: dealing with spaces in paths

Share Button

One useful command line “pattern” is using find and xargs together. For example, to search for instances of “banana” in a folder hierarchy containing text files, one can do this:

If the paths contain spaces, this will fail because xargs by default uses spaces as delimiters. For example, a path  ./folder1/OS Foo/someFile.txt will yield this error:

The solution is to use find’s  -print0 argument, in conjunction with xarg’s  -0  argument:

Used like this, find outputs the path with a terminating ASCII NUL character, and xargs uses NUL as a delimiter, so paths with spaces no longer cause problems