Disks and filesystems
truncate
Shrink or extend a file to an exact size, creating it if needed.
truncate -s [+|-|<|>|/|%]SIZE [OPTION...] FILE...
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
-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
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
-rw-r--r-- 1 dev dev 100M Oct 31 20:21 blank.img 100M blank.img
Holes read as zero bytes
truncate -s 16 holes.bin
xxd holes.bin
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
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
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
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
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
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
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 0destroys 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'scopytruncatedoes).: > fileempties a file too and works in any POSIX shell;truncateadds 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
truncatewith-s,-cand-r; macOS does not ship one (mkfile -n SIZE filecreates a sparse file there). The<,>,/and%prefixes andKB/MBunits are GNU-specific.
Options worth knowing
A selection, described in our own words. For the full list, see truncate(1) or truncate --help.
| Option | What it does |
|---|---|
-s, --size SIZE | Set or adjust the size; prefixes + - < > / % make it relative. |
-r, --reference RFILE | Use RFILE's size. |
-c, --no-create | Do not create files that do not exist. |
-o, --io-blocks | Treat SIZE as a number of I/O blocks instead of bytes. |
Related
ddCopy bytes between files or devices in fixed-size blocks, with optional offsets and conversions.duShow how much disk space files and directories use, totalled per directory.lsList the contents of directories, or the details of individual files.rmDelete files, and with -r whole directory trees. There is no undo.statShow a file's metadata (size, permissions, owner, inode, timestamps), or print chosen fields in your own format.- RedirectionHow >, >>, 2>, 2>&1, &>, < and exec move a command's input and output between files and file descriptors, and why the order matters.