Remix.run Logo
chasil 16 hours ago

I use:

  find . -name '*.log' -print0 | xargs -0 rm
For this simple example (derived from the article), find also has a delete operator.

This null termination is now a POSIX standard.

ivlad 16 hours ago | parent | next [-]

Was exactly my thought: why not to use null termination?

Looks like a case where reading man page would have spared writing another copycat utility.

rmunn 13 hours ago | parent [-]

And null termination is guaranteed to work, because the only two characters forbidden in Unix filenames (for most varieties of Unix, I won't guarantee there aren't some weird variants out there) are / and null.

The only times I've needed something more than `find -print0 | xargs -0` has been when I need to apply logic to decide whether to process one of the files, in a way that's not easy to express in a `find` command. Then I write a small script with a for loop and if statements inside it.

But more people should know about `-print0`. It's the answer to 95% of the problems with `find | xargs`.

Walf 14 hours ago | parent | prev | next [-]

Yeah, and I invariably add -r to xargs, so as not to execute the command unless results come through the pipeline.

If one is working with whole lines of text, setting the delimiter to newline is often desirable:

xargs -d \\n

10 hours ago | parent | prev [-]
[deleted]