truncate
Resize files to specific sizes with the Truncate command. Clear file contents or set exact byte/KB/MB sizes. Free online tool for developers.
Truncate Command
The truncate
command in Linux is a powerful utility
used to shrink or extend the size of a file to a specified size.
This is particularly useful for managing disk space, preparing files
for specific applications, or for testing purposes where a file of a
certain size is required.
File Truncation Basics
The primary function of truncate
is to alter the size
of a file. If the specified size is smaller than the current file
size, the file's content is cut off at that point. If the specified
size is larger, the file is extended, and the new space is typically
filled with null bytes.
Common Truncate Command Usage
Here are some common ways to use the truncate
command:
# To clear the contents from <file> (truncate to 0 bytes):
truncate -s 0 <file>
# To truncate <file> to exactly 100 bytes:
truncate -s 100 <file>
# To truncate <file> to 100 Kilobytes:
truncate -s 100K <file>
# You can also use other units like Megabytes (M), Gigabytes (G), Terabytes (T), etc.
# For example, to truncate to 1 Gigabyte:
truncate -s 1G <file>
Understanding Size Suffixes
The -s
option accepts various suffixes to specify file
sizes conveniently:
B
: Bytes (default if no suffix is given)K
: Kilobytes (1024 bytes)M
: Megabytes (1024 Kilobytes)G
: Gigabytes (1024 Megabytes)T
: Terabytes (1024 Gigabytes)P
: Petabytes (1024 Terabytes)E
: Exabytes (1024 Petabytes)Z
: Zettabytes (1024 Exabytes)Y
: Yottabytes (1024 Zettabytes)
When to Use Truncate
The truncate
command is invaluable for several
scenarios:
- Disk Space Management: Quickly reduce the size of log files or temporary files that have grown too large.
- Testing: Create files of specific sizes for performance testing or application development.
- Data Preparation: Ensure files meet certain size requirements before being processed by other tools.
Further Resources
For more in-depth information on file manipulation and command-line utilities, you can refer to: