Sunday, January 13, 2013

Piping "find" results through xargs

Piping the results of find using xargs doesn't typically work exactly how I want. If a file or folder has a space in its name xargs doesn't handle it correctly.
From the xargs man page:
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.
So, long story short: add "-print0" to find, and "-0" to xargs:

find . -name "*txt" -print0 | xargs -0 grep -i poisson

No comments:

Post a Comment