4 ms·
Not always perfectly right because an argument containing a filename containing a space character will be interpreted as 2 arguments.
by natmaka 2y ago
Not always perfectly right because an argument containing a filename containing a space character will be interpreted as 2 arguments.
- unsnap_biceps 2y agoCertainly, which is why I put quotes around right, but for this usage, it's not an issue. Find prints the whole path on a single line (including the spaces) and grep (by default) puts the full matched line, so you'll still get the full file path regardless of how many spaces are in it.
- CGamesPlay 2y agoNo it won’t, because none of the output is interpreted as an argument. It’s passed as lines to grep. The second invocation correctly uses print0 and pairs with xargs to understand this. Now, it does fail with filenames that have newlines in them, but who would do such a thing!
- natmaka 2y agoI wrote "Not always perfectly right" thinking about all cases, not this particular one: in nearly all cases (bar being absolutely sure there is no blank character anywhere) -print0 (and therefore xargs -0) seems better to me, and it sure saved me on many occasions. Better let "find" do all the work it can, including filtering filenames.
- usr1106 2y agoIf you run this interactively on your own files, saying who would do such thing is fine. If your server code runs this on untrusted input (files uploaded by users or whatever), the answer will be: Someone trying to crack your system.