truncate: Arbitrarily chopping things off

When I mentioned that there were useful and interesting tools in coreutils and util-linux and bsd-games (and I should probably add binutils), I wasn’t exactly thinking of truncate.

2014-09-16-6m47421-truncate

truncate wasn’t on my list when it began five years ago, or even in later additions. I can see why: It’s a rather arbitrary and vicious tool, snapping off files at predetermined lengths and leaving the remainder to flutter away in the wind.

I can’t think of any exact use for truncate aside from determining an exact, to-the-byte length of a file, perhaps for some sort of network testing or disk performance check. And considering the leftovers are summarily discarded, it’s a lethal decision to use it.

truncate follows the same flags for size and units as split and some other toys from coreutils. If you’re familiar with much of what’s in the suite, it will only take you a second to get used to truncate.

And that’s about all I can think of to say about truncate. Use wisely. 😉

2 thoughts on “truncate: Arbitrarily chopping things off

  1. John

    Remember that truncate can expand (by a certain amount or to a specific size) or even create files or a particular size. I’ve known programs that memory map a file and, without checking size, assume it to be an array of size N of structs, so the file should be at least (preferably exactly) that size.

    I’ve also seen it used in scripts to bring a log (or output) file to size zero. You would normally expect that to be > filename, but ksh on the old pa-risc HPUX that would create a new file and the program, if running, would continue writing to the old (now inaccessible) file.

    It’s pretty corner cases, I confess. But the concept of file truncation seems so obvious that there just has to be a utility for it, you know? You have file deletion, creation, moving, writing, reading, etc. etc. … there just has to be a truncate.

    1. K.Mandla Post author

      You’re right on all counts, John, and I should have mentioned the file stretching when I wrote that. I had my mind set on the idea of it chopping things down and didn’t follow the logic there. Thanks for helping. And I’m glad to see truncate actually has some practical uses. I couldn’t think of a darned thing to do with it … 😦 Cheers!

Comments are closed.