Home·Terminal commands

truncate

Files and directories

Usage

truncate -s [size] [file]

Sets a file's length exactly: growing it only makes a sparse hole without allocating space, and shrinking throws the tail away with no warning.

Common flags

FlagMeaning
-sSet the length; a leading + or - grows or shrinks instead
-cDo not create the file if it is missing
-rTake the length from another file
-oGNU: read the size as a number of blocks

Examples

truncate -s 0 app.log

Empties a log while the program keeps writing to it.

truncate -s 1G disk.img

A sparse 1 GB file, created instantly.

truncate -s -100 data.bin

Cuts the last 100 bytes off.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does truncate do?

Sets a file's length exactly: growing it only makes a sparse hole without allocating space, and shrinking throws the tail away with no warning.

Q. How do I type it?

truncate -s [size] [file] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

4 are listed here; the full set is in man truncate. This command sits under Files and directories.

Related commands

man page: man truncate