Linux Command Reference ยท Files and Directories
Linux truncate command
Reference for the Linux truncate command: shrink or extend a file to an exact size, create sparse files, use relative sizes and the -c and -o options.
truncate changes the size of a file to an exact number of bytes. If the file is larger, the extra data at the end is cut off. If it is smaller, it is extended, and the new bytes read as zeros. If the file does not exist, truncate creates it (unless you pass -c).
Extending does not write the zeros to disk; the file system records a hole instead. The result is a sparse file: a large apparent size with almost no space used. That makes truncate the quickest way to make test files, disk images for experiments, or to empty a log file without deleting it.
truncate -s SIZE FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
truncate [-c] [-o] (-s SIZE | -r REFFILE) FILE...
| Option | Description |
|---|---|
-s SIZE, --size=SIZE |
Set the size. A leading + extends by that amount, - shrinks, < never extends (at most SIZE), > never shrinks (at least SIZE), / rounds down to a multiple, % rounds up to a multiple. |
-r REFFILE, --reference=REFFILE |
Use the size of REFFILE. |
-c, --no-create |
Do not create files that do not exist. |
-o, --io-blocks |
Treat SIZE as a number of blocks of the file system, not bytes. |
Size units: a plain number is bytes; suffixes K, M, G, T are powers of 1024 (KB, MB, GB are powers of 1000); KiB, MiB and so on are 1024-based.
Returns: 0 on success; non-zero if a file could not be resized, for example for lack of permission.
Usage notes
- Data is lost when shrinking. There is no undo; the cut-off bytes are gone.
- Use
truncate -s 0to empty a log that a running program still has open, instead ofrm. The program keeps writing to the same inode. If it opened the file withoutO_APPEND, it will write at its old offset and leave a hole, so well-behaved loggers append. - Sparse files: compare
stat -c %s(apparent size) withduor%b(allocated). Copying a sparse file with a naive tool can allocate all the zeros;cp --sparse=alwayskeeps holes. - Not every file system supports holes, and some (FAT) store the zeros for real, so a
truncate -s 10Gcan fill a disk there. - Prefer
-s 0over> filein scripts when you want to say what you mean. They are equivalent in effect, buttruncatefails loudly if the file is not writable. - Mistakes to avoid:
truncate -s 10 *on a directory glob resizes every file; and forgetting-ccreates empty files for typos. - Related file-system call:
ftruncate()/truncate(). If you need a file filled with zeros, useddwithif=/dev/zero.
Related
dd,stat,touch,shred- Lessons: Disk usage, Creating files and directories