Home > OS >  Bash: how to erase the error message produced by xargs from output?
Bash: how to erase the error message produced by xargs from output?

Time:11-13

I have these commands here for looking at files to see if they contain a certain regular expression:

find / -type f | xargs grep -ilIs <regex>

It seems to do what it's supposed to (looking at every file on my desktop for the expression), but ideally i wouldn't show this error message because it's only commenting on unmatched single quotes within a files that the above command finds:

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

I've tried to use sed to get rid of the error message but using | sed '/xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option/d' after the commands does not remove it like i think it should. I'm wondering if any of you know of any tools (ideally with the best ease of reading and least amount of typing, of course) that would get rid of the xargs error message. Including -0 as an argument doesn't return anything except this:

xargs: argument line too long

CodePudding user response:

xargs have it's own escaping syntax, for example:

$ echo 'file 1.txt' | xargs printf '<%s>\n'
<file>
<1.txt>
$ echo '"file 1.txt"' | xargs printf '<%s>\n'
<file 1.txt>

So you CANNOT feed it raw filepaths, because they can contain ANY character but the NUL byte.

For working around that, most implementations of xargs have the -0 switch which allows to process NUL-delimited records, but you need to provide the NUL byte:

$ printf '%s\n' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt
file 2.txt
>
$ printf '%s\0' 'file 1.txt' 'file 2.txt' | xargs -0 printf '<%s>\n'
<file 1.txt>
<file 2.txt>

Finally, you have three ways to do the task correctly:

  1. find ... -print0 | xargs -0 grep ...
find / -type f -print0 | xargs -0 grep -ilIs 'regex'
  1. find ... -exec grep ... {}
find / -type f -exec grep -ilIs 'regex' {}  
  1. grep -R ...
grep -iRlIs 'regex' /
  • Related