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
| Flag | Meaning |
|---|---|
| -s | Set the length; a leading + or - grows or shrinks instead |
| -c | Do not create the file if it is missing |
| -r | Take the length from another file |
| -o | GNU: read the size as a number of blocks |
Examples
truncate -s 0 app.logEmpties a log while the program keeps writing to it.
truncate -s 1G disk.imgA sparse 1 GB file, created instantly.
truncate -s -100 data.binCuts 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