Overcoming filename spaces with git ls-files

by jenny on 31 January 2013 - 01:09am in

If your filenames and/or directory names contain any spaces, it can be a headache to pipe them around in bash. Enter null terminated filenames.

git ls-files -z | xargs -0 grep -li <word>

From git help ls-files:

           \0 line termination on output.

From man xargs:

-0     Input  items  are terminated by a null character instead of by whitespace, and the
        quotes and backslash are not special (every character is taken  literally).   Dis-
        ables  the  end  of file string, which is treated like any other argument.  Useful
        when input items might contain white space, quote marks, or backslashes.  The  GNU
        find -print0 option produces input suitable for this mode.

Of course, in this case, I might have used git grep...

git grep -li <word>