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
-cin 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 exitExamples
Empty a log file without deleting it
truncatex -s 0 app.logCreate a 100 MiB (sparse) file
truncatex -s 100M disk.imgCreate a file of exactly one million bytes
truncatex -s 1MB data.binGrow a file by 1 KiB of zero bytes
truncatex -s +1K data.binRemove the last 10 bytes of a file
truncatex -s -10 data.binCap a file at 1 MiB, leaving smaller files alone
truncatex -s '<1M' cache.dbMake sure a file is at least 4 KiB
truncatex -s '>4K' header.binRound a file's size up to a multiple of 512 bytes
truncatex -s %512 image.rawRound a file's size down to a multiple of 4 KiB
truncatex -s /4K image.rawMake a file the same size as another
truncatex -r original.bin copy.binMake a file 100 bytes larger than another
truncatex -r original.bin -s +100 copy.binEmpty every log that exists, without creating missing ones
truncatex -c -s 0 a.log b.log c.logSize a file in I/O blocks rather than bytes
truncatex -o -s 8 blocks.binCheck the result with wcx
truncatex -s 2K f && wcx -c fOptions
Each option has a page of its own, with its usage and examples.
- -o, --io-blocks: Treat SIZE as number of IO blocks instead of bytes.
- -c, --no-create: Do not create any files.
- -r, --reference: Base size on RFILE.
- -s, --size: Set or adjust the file size by SIZE bytes.
Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: 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