Truncate multiple files at *nix command line
<p>I know how to empty a file's contents without deleting it:</p>

<pre><code>echo "" &gt; catalina.out
</code></pre>

<p>but I want to do it to multiple files in a single command (or command pipeline). What is the most elegant way to do that? </p>

<p><strong>What I've tried so far</strong></p>

<p>Usually I'd experiment with something involving <code>find</code> and <code>xargs</code> but if redirection is part of your xargs command I am not sure if the binding precedence would be interpreted the way I intend. I tried this and it didn't work:</p>

<pre><code>find $TOMCAT_HOME/logs/ -type f | xargs --delimiter "\n" -n 1 -I% echo "" &gt; %I;
</code></pre>

<p>I'd prefer to use find + xargs rather than find -exec or tee + process substitution if possible. (For those that care, my belief is that <code>-exec</code> bypasses the extensibility of pipelines. Maybe not so much in this example but the more corner-cases I can figure out how to use pipes with, the more advanced my understanding of pipelines will be - I think Doug McIlroy's school of thought is underutilized. Also, commands inside strings ruin your editor's syntax highlighting capabilities)</p>

<hr>

<p><strong>Answer</strong> (credits to redneb below)</p>

<p>I find the <code>find -print0 | xargs -0</code> idiom cumbersome, I prefer to use xargs --delimiter. So here's the way I have settled on which is a variation on the accepted answer:</p>

<pre><code>find $TOMCAT_HOME/logs/ -type f | xargs --delimiter "\n" truncate --size=0
</code></pre>

<p>I am also using the long option <code>--size</code> of truncate since self-documenting code is more human-readable.</p>