How To Use “Truncate” Command On Linux?
Introduction
Before we start talking about how to use truncate command on Linux, let's briefly understand-What is a Truncate Command?
The truncate
command on Linux allows you to efficiently truncate or extend the size of a file. Truncating a file means reducing its size to a specific length, while extending a file increases its size. The truncate
command can be useful in various scenarios, such as clearing log files or creating empty files of a specific size.
The excess data will be lost if the file size is more than the allowed size; the truncated size depends on the file's original size.
In this tutorial, we will explore how to effectively use the truncate
command on Linux. We will also address a few FAQs on how to use truncate command on Linux.
Installing Coreutils Packages
The majority of Linux distributions provide the "truncate" command. If not already installed, it can also be done so by executing the following command:
sudo apt-get install coreutils
List the details of packages using the "grep" command:
dpkg –l | grep coreutils
How to use the “truncate” Command?
The most well-liked simple method for truncating files is to use the ">" shell redirection operator.
Syntax
When using redirection, the syntax for truncating files is:
: > filename
The ":" colon indicates true and has no output, and the ">" operator directs output to a particular file.
The file I'm truncating is "test.sh":
: > test.sh
One other method to truncate a file is:
cat /dev/null > test.sh
The contents of the "test.sh" file are being removed.
Clear the Content of File
To delete the files' content, use the "-s" option. Manually deleting a file in this manner is preferable. The truncate command effectively removes all the contents of a file. It leaves the file on the disc as a zero-byte file rather than deleting the file entirely.
Let's truncate file.txt to 0 bytes using this command:
truncate -s 0 file.txt
If you employ the truncate command, the ownership, and permissions of the file will be kept.
To verify the size, type "ls -lh" on your terminal:
ls –lh file.txt
Truncating a File to a Specific Size
Use the subsequent command to make a file:
touch Test.txt
Use the following command to confirm the file permission and size of the file:
ls –lh Test.txt
We will truncate the file size to 100 bytes:
truncate -s 100 Test.txt
Use the following command to confirm the size:
ls -lh Test.txt
Use the following command to truncate a file size to 300K:
truncate -s 300k Test.txt
Use the following command to check the size:
ls -lh Test.txt
By combining the "+" and "-s" options, the file size can be increased. Currently, the file is 300k in size, as seen in the image below:
The file's size should be increased from 300k to 600k bytes:
truncate -s +300k Test.txt
The file has increased in size from 300k to 600k. Verify the size:
ls –lh Test.txt
Reducing the File Size
Suppose you have a 600k file and want to reduce its size to 270k; use the "-s" option and "-" with the size figured:
truncate -s -270k Test.txt
The file's current size is 330k.
Getting Help
Use the following command to get a help message:
truncate --help
Checking Version
Use the following command to check the version of the truncate command:
truncate --version
FAQs: Using the truncate
Command on Linux
How do I use the truncate
command to reduce the size of a file?
To reduce the size of a file using the truncate
command, specify the desired length and the file name as arguments. For example, truncate -s 0 filename
will truncate the file to zero bytes, effectively clearing its contents.
Can the truncate
command be used to increase the size of a file?
Yes, the truncate
command can also be used to increase the size of a file. Use the -s
option followed by the desired size in bytes, kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T).
Can I use the truncate
command on multiple files at once?
Yes, the truncate
command supports truncation or extension of multiple files at once. Simply provide the file names as arguments to the command, separated by spaces.
What happens to the content of a file when it is truncated?
When a file is truncated, its content is removed beyond the specified length. If the file is reduced to zero bytes, the content is completely cleared.
Does the truncate
command modify the creation or modification timestamp of a file?
No, the truncate
command does not modify the creation or modification timestamp of a file. Only the size of the file is changed.
Is it possible to truncate a file to a specific size without deleting its content?
No, when you truncate a file, any data beyond the specified length is deleted. If you want to keep the content but adjust the size, you need to make a backup of the file before truncating it.
Can the truncate
command be used on directories?
No, the truncate
command is intended for manipulating the size of regular files and does not work for directories.
Conclusion:
The truncate
command on Linux offers a straightforward way to modify the size of a file, allowing you to truncate it or extend it to a specific length. By understanding how to use the various options provided by the truncate
command, you can efficiently manage your files, clear data, or create files of desired sizes. Whether you need to manipulate log files, create empty files, or perform other file-related operations, the truncate
command proves to be a valuable tool in your Linux arsenal.
In this tutorial, we learnt how to shrink or extend files, as well as how to truncate a file's content.