2 ms·
What is the most helpful thing you do with xargs and -0?
by tester457 4y ago
What is the most helpful thing you do with xargs and -0?
- dredmorbius 4y agoEasily handling arguments with whitespace is the obvious answer. e.g.: find . -type f -print | xargs wc -l vs. find . -type f -print0 | xargs -0 wc -l If you have filenames with whitespace, the first will skip a bunch of files. If you're doing processing on other inputs which may include whitespace, you'll be able to handle those using xargs without worrying about additional delimiting. And for those not aware, '-print0 / -0' arguments to find(1) and xargs(1) respectively tells the first to output ASCII NUL delimited arguments, and for the latter to expect the same. As ASCII NUL should not normally occur within any argument, it's the ultimate delimiter. I've used xargs and/or bash loops to do some fairly heavy-duty web scraping given an input list of arguments. Using xargs allows multiple simultaneous parallel queries such that any one request stalling out doesn't hold up all process.
- dredmorbius 4y agoAnd to clarify, by "skip a bunch of files", what I of course mean is that if your document is named "Your Document.md", what the first find(1) command will do is attempt to run "wc -l" on "Your" and "Document.md", neither of which it can find, a fact about which it will complain bitterly, creating much noise in your shell session. It will fail to run "wc -l" on the file "Your Document.md", which the second example will do correctly (and not on the name-fragments). "wc -l" gives a line count for a file. That's ... not especially advanced, but is a trivial example of an xargs command which you should be able to try without causing any problems on your own system.