Skip to Content
gnux

truncatex

Shrink or extend the size of each FILE to the specified size.

Mirrors GNU truncate. Run it as truncatex, or as gnux truncatex.

Synopsis

truncatex OPTION... FILE...

GNU reference

Description

truncatex sets the size of each FILE. A FILE that does not exist is created, unless -c is given. A file larger than the size loses the data past it; a file shorter than the size is extended with zero bytes (on most file systems as a sparse hole that takes no disk space). -s SIZE gives the size: a number with an optional suffix (K, M, G, T, P, E for powers of 1024, also written KiB, MiB; KB, MB for powers of 1000), optionally preceded by + (extend by), - (reduce by), < (at most), > (at least), / (round down to a multiple of) or % (round up to a multiple of). -r FILE takes the size from another file; with -s as well, the relative SIZE adjusts the reference size. -o counts SIZE in I/O blocks rather than bytes. One of -s and -r is required. Off Unix the I/O block size used by -o is 512 bytes, because the platform does not report one.

Permissions

Needs write permission on each FILE, and write permission on its directory to create it. -r needs search permission on the reference file's directory only, since only its size is read.

Use cases

  • Emptying a log file in place without changing its owner, mode or open handles.
  • Creating a large sparse file of an exact size for testing or as a disk image.
  • Padding a file to a block multiple with -s %4K.
  • Making a file exactly the size of another with -r.
  • Capping a file at a maximum size with -s '<1M'.
  • Growing a file by a fixed amount with -s +1M.
  • Resizing only files that already exist with -c in a cleanup script.
  • Preallocating a placeholder file before a download fills it.

Flags

      --help              display this help and exit
  -o, --io-blocks         treat SIZE as number of IO blocks instead of bytes
  -c, --no-create         do not create any files
  -r, --reference RFILE   base size on RFILE
  -s, --size SIZE         set or adjust the file size by SIZE bytes
      --version           output version information and exit

Examples

Empty a log file without deleting it

truncatex -s 0 app.log

Create a 100 MiB (sparse) file

truncatex -s 100M disk.img

Create a file of exactly one million bytes

truncatex -s 1MB data.bin

Grow a file by 1 KiB of zero bytes

truncatex -s +1K data.bin

Remove the last 10 bytes of a file

truncatex -s -10 data.bin

Cap a file at 1 MiB, leaving smaller files alone

truncatex -s '<1M' cache.db

Make sure a file is at least 4 KiB

truncatex -s '>4K' header.bin

Round a file's size up to a multiple of 512 bytes

truncatex -s %512 image.raw

Round a file's size down to a multiple of 4 KiB

truncatex -s /4K image.raw

Make a file the same size as another

truncatex -r original.bin copy.bin

Make a file 100 bytes larger than another

truncatex -r original.bin -s +100 copy.bin

Empty every log that exists, without creating missing ones

truncatex -c -s 0 a.log b.log c.log

Size a file in I/O blocks rather than bytes

truncatex -o -s 8 blocks.bin

Check the result with wcx

truncatex -s 2K f && wcx -c f

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

truncatex --help                # options, first examples, exit status
gnux help truncatex             # this page: the full manual
gnux help truncatex --OPTION    # one option in depth, with its examples

See also

On this page