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 Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux pacman Command: Manage Packages on Arch Linux

Linux pacman Command: Manage Packages on Arch Linux

Learn how to manage Packages on Arch Linux with the Linux pacman command, including practical examples, key options, and important precautions.

Linux fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux sed Command: Search and Replace Text Streams

Linux sed Command: Search and Replace Text Streams

Learn how to search and Replace Text Streams with the Linux sed command, including practical examples, key options, and important precautions.