truncate Command Cheat Sheet
truncate shrinks or extends the size of each FILE to the specified size.
Synopsis
truncate OPTION... FILE...
Basic Usage
Set Exact Size (-s)
Set file size to exactly 10 bytes.
truncate -s 10 file.txt
Clear a File (0 bytes)
truncate -s 0 logfile.log
> logfile.log
Relative Resizing
Grow by Size (+SIZE)
Increase file size by 1MB.
truncate -s +1M storage.img
Shrink by Size (-SIZE)
Decrease file size by 1KB.
truncate -s -1K storage.img
Sparse Files
truncate does not actually write zero bytes to disk when extending; it creates a "hole".
truncate -s 10G hugefile.img
du -h hugefile.img # Shows 0 or 4K (actual disk usage)
ls -lh hugefile.img # Shows 10G (apparent size)
Notes
- Permission: Requires write permission on the file.
- Reference: Use
-rto reference another file's size.truncate -r ref_file.txt target.txt