# How to shrink or extend the size of a file in Linux?

LinuxOperating SystemOpen Source

The truncate command is used to shrink or extend the size of a file to the given size. The truncate command cannot remove the file whereas removes the contents of the file and set size of file is zero byte. The meaning of truncate is reducing. While reducing the size of the file is the specified size is less than actual size then extra data will be lost.

## Syntax

The general syntax of the truncate command is as follows.

$truncate OPTION... FILE... Brief description of options available in the truncate command. Sr.No.Option & Description 1-c, --no-create Do not create any files 2-o, --io-blocks Manage size as number of IO blocks rather than bytes. 3-r, --reference=RFILE Set size of file as reference file. 4-s, --size=SIZE Set size of the file to SIZE bytes. 5--help Display this help and exit. 6--version Output version information and exit To set the size of the file from actual size to zero and remove all contents of the file, we use the truncate command as shown in below. $ truncate -s 0 file.txt

After executing this command all contents of the file will be removed.

If we are using truncate command, and file is not available then the truncate create a new file with specified size and name in the Linux system using terminal.

$truncate -s 200K file.txt Here, a file.txt file is not available in the current directory but after execution of this command ‘file.txt’ will be created in current working directory. To pervert creation of new file, we use -c or –no-create option with the truncate command. If file is not available in current directory then new file will not be create using -c or --no -create option with the truncate command in the Linux system. $ truncate -c -s 200K file.txt

## Example

To set the size of the file actual size to specified size, we use the truncate command in the Linux system using terminal as shown in below.

$truncate -s 200K file.txt If the specified size is less than actual size then extra data will be lost. $ truncate --help
Published on 01-Jul-2021 15:36:06