opscape

Disks and filesystems

truncate

Shrink or extend a file to an exact size, creating it if needed.

truncate -s [+|-|<|>|/|%]SIZE [OPTION...] FILE...

10 of 10 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: truncate(1)

truncate sets a file's size. Making it smaller throws away everything past the new end; making it larger adds a "hole" that reads as zero bytes but, on most filesystems, takes no disk space (a sparse file). The data in between is never touched.

Two uses dominate. truncate -s 0 app.log empties a log file in place, so a program that has it open keeps writing to the same file and the space is freed immediately, unlike rm, which leaves the space allocated until the program closes the file. And truncate -s 10G disk.img creates a large placeholder file instantly, for VM disks, swap files on some filesystems, or test data.

Sizes accept suffixes: K, M, G are powers of 1024, KB, MB, GB powers of 1000. A prefix makes the size relative: + grows, - shrinks, < and > cap or ensure a size, and / or % round down or up to a multiple.

Examples

Empty a file in place

truncate -s 0 logs/app.log
ls -l logs/app.log
output
-rw-rw-r-- 1 dev dev 0 Oct 31 20:21 logs/app.log

The file still exists, with the same owner and permissions, but has size 0. Any program writing to it carries on with the now-empty file.

Cut a file down to a number of bytes

truncate -s 20 data/poem.txt
cat data/poem.txt; echo
output
The quick brown fox

Everything after byte 20 is discarded, mid-line if need be; the echo adds the missing final newline for display.

Create a large file instantly

truncate -s 100M blank.img
ls -lh blank.img
du -h --apparent-size blank.img
output
-rw-r--r-- 1 dev dev 100M Oct 31 20:21 blank.img
100M	blank.img

No data is written, so this takes no time. ls and du --apparent-size report 100M; plain du would show almost nothing allocated, since the file is one big hole.

Holes read as zero bytes

truncate -s 16 holes.bin
xxd holes.bin
output
00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................

The extended region reads as zeros, as xxd shows.

Grow a file by a relative amount

wc -c < todo.txt
truncate -s +10 todo.txt
wc -c < todo.txt
output
62
72

A + prefix adds to the current size; - would remove that many bytes from the end.

K versus KB

truncate -s 1K a.bin
truncate -s 1KB b.bin
stat -c '%s %n' a.bin b.bin
output
1024 a.bin
1000 b.bin

K is 1024 bytes, KB is 1000. The same applies to M/MB, G/GB and so on.

Round a size up to a block multiple

truncate -s %512 notes.txt
stat -c '%s %n' notes.txt
output
512 notes.txt

%512 rounds the size up to the next multiple of 512 (190 bytes becomes 512), as some disk image formats and tools require. /512 would round down instead.

Match another file's size

truncate -r access.log copy.bin
stat -c '%s %n' access.log copy.bin
output
2309 access.log
2309 copy.bin

-r FILE takes the size from FILE. Combined with a relative size, -r FILE -s +1K means "one KiB bigger than FILE".

Do not create missing files

truncate -c -s 0 logs/*.log missing.log
ls missing.log
outputexit 2
ls: cannot access 'missing.log': No such file or directory

-c truncates the files that exist and silently skips missing.log instead of creating it, so ls cannot find it afterwards.

Freeing space from a file a process still writes

(while sleep 0.1; do echo "log line $RANDOM"; done >> live.log) &
sleep 1
truncate -s 0 live.log
sleep 0.5
kill $!
wc -l live.log
output
5 live.log

A background loop appends to live.log. After truncate, the loop continues writing to the same file, which now holds only the few lines written since. Because the loop opened the file with >> (append), new lines start at the new end instead of leaving a hole.

Common pitfalls

  • Truncating a file whose writer opened it without append mode (> instead of >>) leaves the writer at its old offset: the next write lands there, and the file jumps back to its old size with a run of zero bytes before the new data.

  • truncate -s 0 destroys the content with no undo. To keep the data, copy it first: cp app.log app.log.1 && truncate -s 0 app.log (what logrotate's copytruncate does).

  • : > file empties a file too and works in any POSIX shell; truncate adds sizes other than zero and many files at once.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • FreeBSD has a truncate with -s, -c and -r; macOS does not ship one (mkfile -n SIZE file creates a sparse file there). The <, >, / and % prefixes and KB/MB units are GNU-specific.

Options worth knowing

A selection, described in our own words. For the full list, see truncate(1) or truncate --help.

OptionWhat it does
-s, --size SIZESet or adjust the size; prefixes + - < > / % make it relative.
-r, --reference RFILEUse RFILE's size.
-c, --no-createDo not create files that do not exist.
-o, --io-blocksTreat SIZE as a number of I/O blocks instead of bytes.

Related