4 ms·
This is a bit off topic I suppose but I'm wondering about lines like this one: ls | grep "roc_nightly.*tar.gz" | xargs rm Isn't this just an over complica
by kwhitefoot 2y ago
This is a bit off topic I suppose but I'm wondering about lines like this one:
ls | grep "roc_nightly.*tar.gz" | xargs rm
Isn't this just an over complicated way of saying:
rm roc_nightly*tar.gz
- SonOfLilit 2y agoIt's a much more generic way of saying it, e.g. you can't use the other way for ls | grep -P "roc_(nightly|weekly).*\.tar\.gz" | xargs rm (forgive me if I got grep -P regex syntax wrong)
- fifticon 2y agoIf you think of the shell as a REPL and an iterative environment, the first variant has many benefits. The first two parts, might be an attempt to see 'can I write a regex to remove the parts we need to get rid of?. I had better run it first, to see it catches the ones I intended. Oops two times in a row, that one didn't catch them all, and that one would include some of those we MUST keep'. I often do the same with SELECT/DELETE on production databases, so I can visually inspect what I'm about to destroy. Transactions don't help me, because if I haven't verified that I'm correct, I won't know that I should use ROLLBACK.
- AnimalMuppet 2y agoOK, but you could still do ls roc_nightly*tar.gz and then replace "ls" with "rm" after you're sure that it gets the right files. (This is very much like your SELECT/DELETE, in fact.)
- williamdclt 2y agoI do that all the time in interactive shells. First because it’s easy to bring back the last command and append to it, second because it gives me more confidence about what I’m actually deleting (I could mess up my rm command if I’m writing it from scratch). With a couple of zsh aliases that becomes quick to write in a stupid-but-simple way: “ls G roc_nightly G tar.gz X rm”
- layer8 2y agoThere can be limits on the size of the command line (ARG_MAX). The pipe formulation circumvents such a limit. See https://en.wikipedia.org/wiki/Xargs#Examples https://en.wikipedia.org/wiki/Xargs#Examples.