← 返回命令列表

Linux command

truncate 命令

安全

复制后可按需替换文件名、目录或参数。

常用示例

Set a file to a specific size

truncate -s [100M] [file]

Empty a file

truncate -s 0 [file]

Shrink a file by

truncate -s -[10K] [file]

Extend a file by

truncate -s +[50M] [file]

Set file to same size

truncate -r [reference_file] [target_file]

Create a sparse file

truncate -s [1G] [sparse_file]

说明

truncate shrinks or extends the size of each FILE to the specified size. If a FILE does not exist, it is created. If a FILE is larger than the specified size, the extra data is lost. If a FILE is shorter, it is extended with zero bytes (creating a sparse file on most filesystems). The command is useful for creating test files of specific sizes, clearing log files without deleting them (preserving permissions and ownership), and creating sparse files for disk image allocation or database pre-allocation. When truncating a file, data beyond the new size is permanently lost. When extending, the file becomes a sparse file where the extended portion doesn't actually consume disk space until data is written to it.

参数

-s _SIZE_, --size=_SIZE_
Set or adjust file size to SIZE bytes
-c, --no-create
Do not create files that don't exist
-o, --io-blocks
Treat SIZE as number of IO blocks instead of bytes
-r _RFILE_, --reference=_RFILE_
Use RFILE's size as the reference size
--help
Display help information
--version
Display version information

FAQ

What is the truncate command used for?

truncate shrinks or extends the size of each FILE to the specified size. If a FILE does not exist, it is created. If a FILE is larger than the specified size, the extra data is lost. If a FILE is shorter, it is extended with zero bytes (creating a sparse file on most filesystems). The command is useful for creating test files of specific sizes, clearing log files without deleting them (preserving permissions and ownership), and creating sparse files for disk image allocation or database pre-allocation. When truncating a file, data beyond the new size is permanently lost. When extending, the file becomes a sparse file where the extended portion doesn't actually consume disk space until data is written to it.

How do I run a basic truncate example?

Run `truncate -s [100M] [file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -s _SIZE_, --size=_SIZE_ do in truncate?

Set or adjust file size to SIZE bytes