Resize Files Safely with truncate
You will use GNU truncate to set a file's exact length, extend it, adjust it relative to its current length, and copy a size from another file. The examples use GNU coreutils 9.4, installed here. That version creates a missing file by default, and shrinking a file permanently discards its tail.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell and write access to a test directory. No example needs elevated privileges. Use sudo only when the file is genuinely owned by another account or protected by its directory permissions; it does not make a destructive resize safer.
1. Check the installed command
Confirm which implementation will run and record its version:
$ command -v truncate
/usr/bin/truncate
$ truncate --version
truncate (GNU coreutils) 9.4
The exact copyright and licence lines can vary. The useful check is the GNU coreutils version. This guide follows the installed truncate(1) manual, dated August 2026.
Checkpoint: if command -v finds a different executable, read that implementation's manual before using these options. The short options and size rules here are GNU behaviour.
2. Back up before shrinking
Setting a smaller size removes bytes from the end of the file. That loss is not undone by setting a larger size later: the new part reads as zero bytes rather than restoring the old data.
Make a same-filesystem copy before a resize you may need to reverse:
$ cp --preserve=all /path/to/data.bin /path/to/data.bin.before-truncate
$ wc -c < /path/to/data.bin
1048576
The backup consumes storage and should be protected as carefully as the original. If the file is a database, log, image or other format with its own consistency rules, use that application's backup or rotation procedure as well. A byte-level copy is not automatically an application-level backup.
3. Set an exact size
Use -s or --size followed by a size and one or more file names. An unadorned integer is a byte count:
$ truncate --size 4096 /path/to/data.bin
$ wc -c < /path/to/data.bin
4096
Units use powers of 1024 when written as K, M, G and so on. Decimal forms such as KB use powers of 1000. Binary spellings such as KiB are accepted too:
$ truncate --size 2M /path/to/data.bin
$ stat --format='%s bytes' /path/to/data.bin
2097152 bytes
Use stat when you want a direct size check without counting the file contents. A size larger than the current length extends the file. The extended region is a sparse hole and reads as zero bytes; the filesystem may not allocate physical blocks for it immediately.
4. Adjust rather than replace the current size
Prefix the size with + to extend by that amount or - to reduce by that amount:
$ stat --format='%s bytes' /path/to/data.bin
2097152 bytes
$ truncate --size +512K /path/to/data.bin
$ stat --format='%s bytes' /path/to/data.bin
2621440 bytes
$ truncate --size -1M /path/to/data.bin
$ stat --format='%s bytes' /path/to/data.bin
1572864 bytes
A reduction still discards data. In scripts, check the current size and keep a backup before applying a negative adjustment. Do not confuse --size -1M with a command-line option: the argument belongs to --size, so this form is valid.
5. Avoid accidental file creation
If a named file does not exist, GNU truncate creates it by default. That is useful for preparing a sparse placeholder, but it is an easy typo to miss. Add --no-create when the target must already exist:
$ truncate --no-create --size 4096 /path/to/maybe-missing.bin
truncate: cannot open '/path/to/maybe-missing.bin' for writing: No such file or directory
The diagnostic wording can differ with the locale. The important result is a non-zero exit status and no new file. This check does not protect you from naming an existing file incorrectly, so inspect the target path before a destructive operation.
6. Copy a size from another file
Use --reference when the target should have the same length as a known file. The reference file's contents are not copied:
$ stat --format='%n: %s bytes' /path/to/reference.bin /path/to/data.bin
/path/to/reference.bin: 8192 bytes
/path/to/data.bin: 1572864 bytes
$ truncate --reference=/path/to/reference.bin /path/to/data.bin
$ stat --format='%n: %s bytes' /path/to/reference.bin /path/to/data.bin
/path/to/reference.bin: 8192 bytes
/path/to/data.bin: 8192 bytes
This can shrink the target, so apply the same backup rule. If you need the reference data as well as its length, use cp, not truncate.
7. Handle shell metacharacters and block units
Size modifiers include < for at most, > for at least, / to round down to a multiple and % to round up. Quote these values so the shell does not treat them as redirection or other syntax:
$ truncate --size '<10M' /path/to/data.bin
$ truncate --size '/4K' /path/to/data.bin
The first command limits the file to 10 MiB if it is larger; otherwise its size is unchanged. The second rounds down to a multiple of 4096 bytes. These modifiers are size rules, not content operations.
--io-blocks makes the size count I/O blocks instead of bytes. Do not use it casually: the result depends on the file's I/O block size and is easy to misread in a review. Prefer byte or explicit unit sizes unless the storage operation specifically calls for block counts.
8. Restore a file if the resize was wrong
If you made the backup above, restore it only after checking the path and that no newer work has been written to the target:
$ stat --format='%n: %s bytes' /path/to/data.bin.before-truncate
$ cp --preserve=all /path/to/data.bin.before-truncate /path/to/data.bin
$ stat --format='%n: %s bytes' /path/to/data.bin
This replaces the target with the saved copy. If the file is actively written by a service, stop or quiesce that service according to its documentation first. Do not restore over a live file and assume the application will reconcile the change.
Done means
truncate --versionidentifies the implementation you checked.- You measured the target before and after the operation.
- You backed up before shrinking or using a reference that might be smaller.
- You used
--no-createwhen a missing path must be an error. - You know how to restore the backup, and any service using the file is in a safe state.