Sometimes you may need to truncate the contents of file in Linux. This is especially required if you need additional disk space on your system. Linux provides truncate command that allows you to easily truncate files. In fact, it allows you to completely clear file content, reduce file size, or even expand file size, making it really useful. In this article, we will look at how to truncate file in Linux.
How to Truncate File in Linux
Truncate command is commonly used to shrink or expand files. If you shrink a file, some of the file contents are lost. If you expand a file, then additional space is filled with 0 bytes of data. It is generally present in almost every Linux system. However, if it is not the case on your system, then open terminal and run the following command to install it.
Ubuntu/Debian
$ sudo apt-get install coreutils
RHEL/CentOS/Fedora
$ sudo yum-y install coreutils
Here is the syntax of truncate command.
$ truncate [option] file_path
Here are the commonly used options
-c - do not create any files -o - treat SIZE as number of IO blocks instead of bytes -s - set or adjust the file size by SIZE bytes --help - display this help and exit --version - output version information and exit
When you use -s option to set the file to a specific size, you need to mention the new file size as an integer followed by an optional unit of K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB,…(as powers of 1000).
You may also prefix the size with following operators such as
- ‘+’ extend by
- ‘-‘ reduce by
- ‘<‘ at most
- ‘>’ at least
- ‘/’ round down to multiple of
- ‘%’ round up to multiple of
Now we will look at some common use cases of truncate command.
Clear File Contents using Truncate
Here is a simple command to clear file /home/ubuntu/data.txt using truncate.
$ sudo truncate -s 0 /home/ubuntu/data.txt
This is a convenient way to clear log files that regularly grow in size. It does not remove the file but clears all its contents and reduces it to 0 bytes. Also, it will not change file permissions and ownership in the process.
Truncate File to Specific Size
Here is the command to truncate file to specific size such as 1000 bytes.
$ sudo truncate -s 1000 /home/ubuntu/data.txt
Here is a command to truncate file to 100Kb.
$ sudo truncate -s 100K /home/ubuntu/data.txt
Extend or Shrink file by specific size
If you want to truncate the file by specific size, say 100K, then use – operator before the size argument, as shown below.
$ sudo truncate -s -100K /home/ubuntu/data.txt
Please note the – operator before 100k in the above command. It will decrease the file size by 100kb. If you omit the – operator, it will set the file size to 100k. There is a big difference in both the commands just by including/excluding an operator.
Similarly, if you want to increase the file by 100Kb just add + operator before the size argument.
$ sudo truncate -s +100K /home/ubuntu/data.txt
Here also please note the + operator before 100k in the above command. It will increase the file size by 100kb. If you omit the + operator, it will set the file size to 100k.
That’s it. In this article, we have learnt how to install truncate command. We have also seen how to clear file, set file to specific size, shrink or expand a given file using truncate command.
If you want to regularly perform these operations, you can always add them to a shell script or setup a cron job to regularly run it. For example, open crontab using the following command.
$ crontab -e
Add the following command to clear log file /home/ubuntu/server.log file on 1st of every month at 10.a.m. You may modify the following command as per your requirement, or generate your own cronjob command with a crontab generator.
0 10 1 * * sudo truncate -s 0 /home/ubuntu/server.log >/dev/null 2>&1
Also read:
How to Do Reverse DNS Lookup in Linux
How to SSH Using PEM File in Ubuntu
SCP Command to Copy File From Server to Another
How to Read YAML File to Dict in Python
How to Delete Empty Lines from Text File in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.