The “truncate” command is utilized to extend or shrink the size of the targeted file to a particular size. It also facilities Linux users by extending or reducing the original file size as per requirements. It allows truncating the file content without affecting its size.
Considering its importance, this post will explain Linux’s working and usage of the “truncate” command.
The guideline of this post is listed below:
- Prerequisites: Install “truncate” Utility
- How to Use the “truncate” Command in Linux?
- Delete the file content
- Truncate a File to the Desired Size
- Extend the Size of a Particular File
- Decrease the Particular File Size
Let’s start with installing the “truncate” command.
Prerequisites: Install “truncate” Utility
The “truncate” command line tool is pre-installed in most Linux distributions. If it is not installed, It can be installed by utilizing the below-mentioned command:
$ sudo apt install coreutils #For Debian/Ubuntu-based
$ sudo dnf install coreutils #For Fedora-based
$ sudo pacman -S coreutils #For Arch-based
It is installed here.
How to Use the “truncate” Command in Linux?
The working of the “truncate” command depends upon its syntax. The generalized syntax of the “truncate” command is written below.
Syntax
$ truncate [Option] <Filename>
This syntax contains the following components:
- truncate: Represents the “truncate” command.
- Filename: Identifies the targeted file.
The truncate command also provides a limited list of arguments that can easily get through its “help” command in the following way:
$ truncate --help
Now, move on to the practical implementation of the “truncate” command with the help of various practical examples.
Example 1: Delete the File Content
The “truncate” command clears the file content but does not delete the actual file. Suppose the “Test.txt” file contains the following content:
$ cat Test.txt
Use the is the “truncate” command to delete or remove the “Test.txt” file content:
$ truncate -s 0 Test.txt
As the above command has successfully deleted the “Test.txt” file content.
Note: The “-s 0” in the above command shows the “Test.txt” file size “0”. This is the necessary argument required during the deletion process of a file or its content.
Example 2: Truncate a File to the Desired Size
The “truncate” command is also beneficial for truncating a file to the desired file size. As the Original file size of the “First.txt” file is “1.5KB”:
$ ls -lh First.txt
Execute the “truncate” command to truncate the size of the “First.txt” file from “1.5KB” to “10b” in this way:
$ truncate -s 0 First.txt
The output of “ls -lh” command verifies that the file size of “First.txt” was truncated from “1.5KB” to “10b”.
Example 3: Extend the Size of a Particular File
The operator “-” is generally used to shrink the file size, while the “+” helps extend the file size. Here, in this case, the “+” operator will extend the particular file “Extra.txt” having original size:
$ ls -lh Extra.txt
Extend the “Extra.txt” file size from “1.6K” upto “2K” using the “truncate” command:
$ truncate -s +2K Extra.txt
Here, the “Extra.txt” file size has been extended from “1.6K” to “3.6K” after adding the “2K”.
Example 4: Decrease the Particular File Size
The user can decrease the file size by using the “-” operator with the specified file size. In this case, a “Test.txt” file is taken whose actual size is:
$ ls -lh Test.txt
Decrease its actual size “972b” upto the “100b” with the help of truncate command in this way:
$ truncate -s -100 Test.txt
The “Test.txt” file size has been reduced from “972b” to “872b” after decreasing the “100b” from it.
That’s how you can use the truncate command in Linux.
Conclusion
The main objective of the “truncate” Linux command is to reduce/shrink or extend the standard file size to the required one. This command is also beneficial to clear the content of the file without deleting it from the system. This post has briefly illustrated the objective, functionalities, and working of the “truncate” command in Linux.