[PLUG] One-liners to empty all files in a directory?

Sam Hart criswellious at gmail.com
Mon Aug 15 22:47:18 UTC 2011


On Mon, Aug 15, 2011 at 6:22 PM, Jameson Williams
<jameson.h.williams at intel.com> wrote:
> I haven't been able to get this one yet.
>
> Challenge: A one-line statement (pipes okay, but explicit loops not)
> that empties all found files (as for debugging with /var/log, perse).
>
> This is close, but has a loop:
>
>     find -type f | while read file; do :>$file; done
>
> This seems like it might work, but doesn't:
>
>     find -type f -exec cat /dev/null \> {} \;

Seems like a silly question as there's *no* way to technically avoid
loops (even if you're not doing it in your line, the underlying code
is using a loop), but I'd probably use xargs and truncate:

ls | xargs truncate --size=0

Use find instead of ls if you want something more fancy like
recursion. Might be pretty fun to do as root on / :-)

                                      ---Sam



More information about the PLUG mailing list