GNU head
and tail
since coreutils version 8.25 have a -z
option for that.
With older versions or for non-GNU systems, you can try and swap \0
and \n
:
find ... -print0 |
tr '\0\n' '\n\0' |
head |
tr '\0\n' '\n\0'
Note that some head
implementations can't cope with NUL characters (and they're not required to by POSIX), but where find supports -print0
, head
and text utilities generally support NUL characters.
You can also use a function to wrap any command between the two tr
s:
nul_terminated() {
tr '\0\n' '\n\0' | "$@" | tr '\0\n' '\n\0'
}
find ... -print0 | nul_terminated tail -n 12 | xargs -r0 ...
Keep in mind that under nul_terminated
, a \0
means a newline character. So for instance, to replace \n
with _
:
find . -depth -name $'*\n*' -print0 | nul_terminated sed '
p;h;s,.*/,,;s/\x0/_/g;H;g;s,[^/]*\n,,' | xargs -r0n2 mv
(\x0
being also a GNU extension).
If you need to run more than one filtering command, you can do:
find ... -print0 |
nul_terminated cmd1 |
nul_terminated cmd2 | xargs -r0 ...
But that means running a few redundant tr
commands. Alternatively, you can run:
find ... -print0 | nul_terminated eval 'cmd1 | cmd2' | xargs -r0 ...