LinuxCommandLibrary

truncate

Reduce or extend file size

TLDR

Set a size of 10 GB to an existing file, or create a new file with the specified size

$ truncate [[-s|--size]] 10G [path/to/file]
copy

Extend the file size by 50 MiB, fill with holes (which reads as zero bytes)
$ truncate [[-s|--size]] +50M [path/to/file]
copy

Shrink the file by 2 GiB, by removing data from the end of file
$ truncate [[-s|--size]] -2G [path/to/file]
copy

Empty the file's content
$ truncate [[-s|--size]] 0 [path/to/file]
copy

Empty the file's content, but do not create the file if it does not exist
$ truncate [[-cs|--no-create --size]] 0 [path/to/file]
copy

SYNOPSIS

truncate [OPTION]... FILE...
truncate -s SIZE [OPTION]... FILE...

PARAMETERS

-s, --size=SIZE
    Set or adjust the file size to SIZE bytes.
SIZE can be followed by units:
K, M, G, T, P, E, Z, Y (powers of 1024, e.g., 10K is 10 * 1024).
KB, MB, GB, TB, PB, EB, ZB, YB (powers of 1000, e.g., 10KB is 10 * 1000).
KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB (explicitly binary powers of 1024, e.g., 10KiB is 10 * 1024).

-c, --no-create
    Do not create any files that do not exist. If a specified FILE does not exist, truncate will report an error and exit.

--reference=RFILE
    Set the size of the target FILE to that of RFILE. The size is determined by the size of RFILE.

-r, --relative[=OP]
    Adjust the size relative to the current file size. OP can be + (add), - (subtract), < (truncate if larger), or > (extend if smaller). If OP is omitted, it defaults to +. For example, --relative=+1G adds 1 Gigabyte to the file's current size.

-o, --io-blocks
    Interpret SIZE as the number of I/O blocks instead of bytes. This option is less commonly used as byte-based sizing is generally more precise and common.

--help
    Display a help message and exit.

--version
    Display version information and exit.

DESCRIPTION

The truncate command-line utility is used to precisely control the size of a file. It can both shrink a file, discarding any data beyond the new specified length, or extend it, appending null bytes (zeros) or creating "holes" to reach the desired size.

This operation is highly efficient for large files as it modifies the file's metadata directly, avoiding the need to read or write the entire file content. It's commonly employed for managing log files, pre-allocating space for future data, or creating sparse files to optimize disk usage. It provides a convenient user-level interface to the underlying ftruncate(2) system call.

CAVEATS

Truncating a file to a smaller size irrevocably discards any data beyond the new size. There is no undo for this operation.

Extending a file often results in sparse files. This means the newly allocated space might not consume actual disk blocks until data is explicitly written to them, saving disk space. However, some filesystems may allocate physical blocks immediately.

The user executing truncate must have write permissions on the target file(s) and potentially the directory if creating a new file.

truncate does not operate on directories; it is designed solely for regular files.

SPARSE FILES

One of the key uses of truncate, particularly when extending a file, is the creation of sparse files. When truncate extends a file, it typically creates 'holes' in the file rather than physically writing zero bytes to the new space. These holes do not consume actual disk blocks until data is explicitly written into them. This behavior is highly beneficial for large files that are expected to grow incrementally, as it conserves disk space.

HISTORY

The truncate command is a utility provided by GNU Coreutils, acting as a user-friendly wrapper for the underlying ftruncate(2) system call. Its core functionality of resizing files has remained consistent since its inception. Over time, features like relative sizing (--relative) and reference file sizing (--reference) were added to enhance its flexibility and make it a more versatile tool for file system management.

SEE ALSO

ftruncate(2), dd(1), fallocate(1), ls(1)

Copied to clipboard