0
$ cat Makefile
clean:
    find . -print0 | grep -z pattern | tee >\(tr '\0' '\n' > /dev/tty\) | xargs -0 rm
$ make clean
/bin/sh: /dev/tty): Permission denied

Previous question:

How can I display intermediate pipeline results for NUL-separated data?

cjfp
  • 153
  • 1
  • 9
  • By default, make uses `/bin/sh`, which means you don't have bash-only syntax such as process substitution. – Charles Duffy Jun 23 '17 at 23:34
  • BTW, is there a reason you're using `grep`, rather than telling `find` to exclude files matching the pattern itself? That would moot the need for a process substitution at all, since you could run `find . -path '*pattern*' -prune -o -print -exec rm -- {} +` or such. (Of course, I'd advise using `-name` rather than `-path` if possible). – Charles Duffy Jun 23 '17 at 23:36
  • 1
    Actually, turns out we already have an on-point question and answer. :) – Charles Duffy Jun 23 '17 at 23:54
  • @CharlesDuffy I'm using two grep commands to exclude some things with -v, and include other things without -v. But also laziness, I avoided learning all the ins and outs of the swiss-army knives. And I like composing pipelines with simple pieces. Can you do `find . | grep -v p1 | grep p2 | xargs` with just find? Agreed that make syntax is kind of terrible, I probably should have stuck with the multi-line rules. Still, I learned something today... – cjfp Jun 23 '17 at 23:59
  • Yes, you can do both positive and negative tests in the same `find` invocation. `find . -path p1 ! -path p2 -exec ...` – Charles Duffy Jun 24 '17 at 00:04
  • http://mywiki.wooledge.org/UsingFind is probably a good place to start. – Charles Duffy Jun 24 '17 at 00:07
  • Cheers. I deleted my previous comment about learning find, but that was before I saw yours... – cjfp Jun 24 '17 at 00:10
  • except, of course, for section 7.1, "Nasty OS X bug", precluding the use of -exec ... + on my system – cjfp Jun 24 '17 at 00:19
  • 1
    That bug really matters to you for your current use case? When is `rm -f` going to fail? And you can, of course, use `-exec ... {} \;` instead (at a performance penalty, but nonetheless). – Charles Duffy Jun 24 '17 at 00:50
  • Fair enough. I guess my gut reaction if something isn't going to work consistently is to avoid using it, but it's not always appropriate. In this case it seems the best thing is to do is to just install findutils. – cjfp Jun 24 '17 at 01:25

0 Answers0