Linux truncate Command: Shrink or Extend File Size

This guide explains how to shrink or extend files with Linux truncate, adjust sizes relatively, match a reference file, and distinguish sparse logical size from disk usage. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.

The examples use GNU Coreutils behavior. Check truncate --help, man truncate, and your distribution documentation when portability or a version-specific option matters.

What Is the truncate Command?

truncate shrinks or extends the logical length of files. Shrinking discards bytes beyond the new end. Extending usually creates a sparse region that reads as zero without allocating every block immediately. Missing files are created unless -c is used.

Basic Syntax

truncate [OPTION]... FILE...

Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.

Basic truncate Usage

Reduce a file to zero bytes

The directory entry and attributes remain while the content length becomes zero. Use application-aware rotation rather than truncating active service logs blindly.

truncate -s 0 application.log

Create a file with a specified logical size

Suffixes such as M and GiB define the requested size. The resulting file may be sparse.

truncate -s 100M test.img
truncate -s 1GiB sparse.img

Grow or shrink relative to the current size

A leading plus grows the file and a leading minus shrinks it by the specified amount.

truncate -s +10M data.bin
truncate -s -512K data.bin

Check the size after clearing the content

truncate usually does not display a message even if it succeeds. Applying -s 0 to an existing file will set its byte size to 0.

printf 'abc' > demo.txt
truncate -s 0 demo.txt
stat -c '%s %n' demo.txt

An example output is 0 demo.txt. The file content cannot be recovered, so make sure to check backups and target paths before using it on actual logs or data files.

Key Options

Option Purpose
-s SIZE Set or adjust the size.
-c Do not create missing files.
-r FILE Use the size of a reference file.
-o Interpret the size in the file's I/O blocks instead of bytes.

Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.

Practical Examples

Match a reference file's size

The -r option copies only the size, not the contents.

truncate -r reference.img target.img
stat -c '%s %n' reference.img target.img

Apply a maximum or minimum size

GNU size prefixes can change a file only when it is above or below a threshold. Quote shell-sensitive prefixes.

truncate -s '<100M' cache.bin
truncate -s '>1M' placeholder.bin

Compare logical size and allocated space

An extended area can be a sparse hole. Use stat and du to distinguish logical length from allocated storage.

truncate -s 1G sparse.img
stat -c 'size=%s blocks=%b' sparse.img
du -h sparse.img

truncate and Related Commands

Command or method Primary role Best fit
truncate Change the logical end of a file Fast resizing and sparse-file creation
touch Create an empty file or change timestamps Preserving existing size and content
dd Write or copy actual data blocks Populating contents or imaging devices
Log rotation tool Archive, compress, and coordinate log reopening Production service logs

Important Cautions

Shrinking destroys data

Bytes beyond the new end are lost. Work on a backup when the file matters.

Extending does not repair an internal format

Increasing an image or filesystem container's length does not expand its internal structures. Use the format-specific tool.

Sparse files can expand during copying

A copy tool or destination filesystem that does not preserve holes can consume the full logical size.

FAQ

Does truncate -s 0 delete the file?

No. It keeps the file entry and attributes while setting the length to zero.

Does extending a file allocate all of that disk space?

Not necessarily. The extension can be sparse; compare stat and du.

How do I avoid creating a missing file?

Use truncate -c -s SIZE FILE.

Does -r copy data from the reference?

No. It copies only the reference file's size.

Related Resources

More in This Category
Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux Tutorial / Hard Links vs. Symbolic Links in Linux: Differences and Examples

Linux Tutorial / Hard Links vs. Symbolic Links in Linux: Differences and Examples

Compare hard and symbolic links through inode and path behavior, then create both kinds of link and observe what happens when the original name changes.

Linux touch Command: Create Files and Change Timestamps

Linux touch Command: Create Files and Change Timestamps

Learn how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

Linux sync Command: Flush Buffered Writes to Storage

Linux sync Command: Flush Buffered Writes to Storage

Learn how to flush Buffered Writes to Storage with the Linux sync command, including practical examples, key options, and important precautions.

Linux Tutorial / Read Files with cat, less, more, head, and tail

Linux Tutorial / Read Files with cat, less, more, head, and tail

Learn when to use cat, less, more, head, and tail to read Linux files, with a small reproducible example and guidance for long files and logs.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.