How to use the command 'truncate' (with examples)
The ’truncate’ command is a utility used to modify the size of a file. It allows you to shrink or extend the size of a file to the specified size. By using the ’truncate’ command, you can set the size of an existing file or create a new file with a specific size. This command is particularly useful when you want to allocate a certain amount of space for a file or clear its content.
Use case 1: Set a size of 10 GB to an existing file, or create a new file with the specified size
Code:
truncate --size 10G filename
Motivation: This use case is helpful when you want to allocate a specific size of storage for a file, ensuring that it has enough space for your needs. For example, if you are working with large datasets, you may want to create a blank file with a certain size to accommodate the data you will be working with.
Explanation:
--size 10G
: Specifies the desired size for the file. In this case, the size is set to 10 gigabytes.
Example output: If the file already exists, its size will be truncated to 10 gigabytes. If the file doesn’t exist, a new blank file with a size of 10 gigabytes will be created.
Use case 2: Extend the file size by 50 MiB, fill with holes
Code:
truncate --size +50M filename
Motivation: This use case is useful when you need to increase the size of a file while maintaining its existing content. It can be helpful in scenarios where you want to allocate more space to a file for future data without overwriting any existing data.
Explanation:
--size +50M
: Specifies the size increment for the file. In this case, the file size will be increased by 50 mebibytes.filename
: Specifies the name of the file to be modified.
Example output: The file size will be extended by 50 mebibytes, and the additional space will be filled with holes, which are represented as zero bytes. The existing content of the file will remain unchanged.
Use case 3: Shrink the file by 2 GiB, removing data from the end of the file
Code:
truncate --size -2G filename
Motivation: This use case is handy when you want to reduce the size of a file and remove some of its content. It can be useful in situations where you have a file with unnecessary data at the end that you want to eliminate.
Explanation:
--size -2G
: Specifies the size reduction for the file. In this case, the file size will be decreased by 2 gibibytes.filename
: Specifies the name of the file to be modified.
Example output: The file size will be decreased by 2 gibibytes, and the data at the end of the file will be removed. The remaining content of the file will remain intact.
Use case 4: Empty the file’s content
Code:
truncate --size 0 filename
Motivation: This use case is useful when you want to completely remove all the content from a file and make it empty. It can be helpful in scenarios where you want to start fresh with a file or clear sensitive or unnecessary data.
Explanation:
--size 0
: Specifies the desired size of the file, which is 0 in this case.filename
: Specifies the name of the file to be modified.
Example output: The content of the file will be completely removed, making it an empty file. The file will still exist, but it will have no content.
Use case 5: Empty the file’s content, but do not create the file if it does not exist
Code:
truncate --no-create --size 0 filename
Motivation: This use case is useful when you want to empty the content of an existing file, but if the file does not exist, you do not want to create a new file. It can be beneficial when you only want to modify existing files and avoid accidentally creating new ones.
Explanation:
--no-create
: Prevent the command from creating a new file if the specified file does not exist.--size 0
: Specifies the desired size of the file, which is 0 in this case.filename
: Specifies the name of the file to be modified.
Example output: If the file exists, the content will be removed, making it an empty file. If the file does not exist, no action will be taken, and the command will not create a new file.