Friday, April 20, 2012

find | xargs with spaces in filenames

The following command is one of the simplest way to find a text in the files.
    find * | xargs grep "text"
but if the file name contains a space then the xargs command split it and transfer spited items as a separate files to the grep, and an error occurs:
    grep: "a part of file name": No such file or directory

To overcome this issue the find has an -print0 option, which separates file names with ASCII NUL instead of new line. And the -0 option will signal the xargs to use NUL as a separator instead of blank spaces.
    find * -print0 | xargs -0 grep "text"

No comments:

Post a Comment