Hacker News new | past | comments | ask | show | jobs | submit login

Not always perfectly right because an argument containing a filename containing a space character will be interpreted as 2 arguments.



No 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!


I 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.


If 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.


Certainly, 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.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: