Managing storage volumes on Linux servers frequently requires shrinking overgrown log files, creating pre-allocated sparse disk image containers for virtual machines, or resetting diagnostic database files to zero bytes without deleting and recreating directory permissions. While administrators often rely on redirection operators (such as > file.log) or the dd utility, shell redirection lacks sizing controls, and dd commands are lengthy and error-prone.
The truncate command, included in the core Linux coreutils package, provides a fast, dedicated command-line tool for shrinking, expanding, or zeroing out files to an exact byte size. Rather than reading and rewriting physical disk sectors, truncate adjusts file metadata headers directly via the kernel’s truncate() and ftruncate() system calls, completing operations instantaneously regardless of file volume.
Zeroing Out an Existing File Without Breaking File Descriptors
When an active background daemon (such as Nginx, Apache, or a database server) is continuously writing to a multi-gigabyte log file, deleting the file with rm does not reclaim disk space because the operating system maintains open file descriptors. The space is only freed after restarting the daemon.
To immediately reclaim disk space while keeping the file descriptor intact and avoiding service restarts, truncate the file to zero bytes:
sudo truncate -s 0 /var/log/nginx/access.log
The file size instantly collapses to zero bytes, and the daemon continues writing incoming logs without disruption.
Resizing Files to Specific Exact Dimensions
The -s (or --size) option accepts standard human-readable unit suffixes:
K: Kilobytes (1024 bytes)M: Megabytes (1024 * 1024 bytes)G: Gigabytes (1024 * 1024 * 1024 bytes)T: Terabytes
Examples of exact sizing operations:
# Shrink or expand a file to exactly 50 Megabytes
truncate -s 50M data_buffer.bin
# Resize an archive file to exactly 1 Gigabyte
truncate -s 1G disk_image.raw
Relative File Expansion and Shrinking
You can adjust file dimensions relative to their current size using prefix modifier symbols:
+: Extends the file by the specified increment.-: Reduces the file by the specified decrement.<: Slices the file to at most the specified size (only shrinks if larger).>: Extends the file to at least the specified size (only expands if smaller).
# Reduce a large log file by exactly 100 Megabytes from its end
truncate -s -100M app_debug.log
# Ensure a container file is at least 500 Megabytes
truncate -s >500M cache_pool.dat
Creating Sparse Files for Testing and Virtual Disks
When you expand a non-existent or small file using truncate, the Linux filesystem creates a sparse file. Sparse files allocate virtual file boundaries in directory metadata without occupying physical storage sectors on disk until real data is written:
# Create a 10 GB virtual disk container instantaneously
truncate -s 10G virtual_disk.img
You can verify that the file reports a virtual size of 10 GB while consuming zero physical disk blocks using ls and du:
# View apparent virtual size (shows 10G)
ls -lh virtual_disk.img
# View actual physical disk space allocated (shows 0)
du -sh virtual_disk.img
Preventing File Creation with -c Flag
By default, if the targeted filename does not exist, truncate creates it automatically. If you only want to modify files that already exist on disk without creating unintentional empty files, pass the -c (or --no-create) flag:
truncate -c -s 0 /var/log/custom_service.log
Summary of Common truncate Command Flags
| Flag | Option Name | Functional Description |
|---|---|---|
-s 0 |
--size 0 |
Empties file contents completely without deleting the file. |
-s <size> |
--size <size> |
Sets the file to the exact specified byte dimension. |
-s +<size> |
--size +<size> |
Appends sparse null bytes to extend file length. |
-s -<size> |
--size -<size> |
Truncates trailing bytes to shrink overall file size. |
-c |
--no-create |
Suppresses file creation if the target does not exist. |
The truncate utility delivers a safe, high-speed solution for file management, log maintenance, and test environment container provisioning across all Linux distributions.