Welcome to our tutorial on how to use truncate command in Linux. The Linux truncate command is often used to shrink or extend the size of each FILE to the specified size. The final size of the file depends on the initial. If a FILE (for example archive or log files) is larger than the specified size, the extra data is lost, but if a FILE is shorter, it is extended and the extended part (hole) reads as zero bytes.
The commands shown were tested on CentOS 7 machine and Ubuntu 16.04 server. Most Linux distributions ship with truncate command. If your system doesn't have a truncate command, for Ubuntu/ Debian system, it is provided by the coreutils package.
$ sudo apt-get install coreutils $ dpkg -l | grep coreutils coreutils 8.25-2ubuntu3~16.04 amd64 GNU core utilities
For CentOS 7:
# yum provides truncate coreutils-8.22-18.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts Repo : base Matched from: Filename : /usr/bin/truncate
If not installed, use:
$ sudo yum-y install coreutils
Common truncate options
-c, --no-create --> do not create any files -o, --io-blocks --> treat SIZE as number of IO blocks instead of bytes -r, --reference=RFILE --> base size on RFILE -s, --size=SIZE --> set or adjust the file size by SIZE bytes --help --> display this help and exit --version --> output version information and exit
When using truncate, the SIZE argument has to be an integer and optional unit (example: 10K is 10*1024). The Units as used are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB,...(as powers of 1000).
The SIZE argument may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.
Examples of truncate usage
Below is the most common usage examples of the truncate command.
1. Clear contents of a file with truncate
# truncate -s 0 file
This is useful e.g for log clearing files. This is better than manually deleting a file and maybe doing a touch for the new one. The truncation process basically removes all the contents of the file. It does not remove the file itself, but it leaves it on the disk as a zero byte file. As an example, let's clear our /var/log/syslog to 0 bytes using truncate.
# du -sh /var/log/syslog 616K /var/log/syslog # truncate -s 0 /var/log/syslog
If we do a check again for the file size, it should be 0 bytes.
# du -sh /var/log/syslog 0 /var/log/syslog
Note that truncate command will retain file permissions and ownership. You can confirm by using ls -lh commands:
# ls -lh /var/log/syslog -rw-r----- 1 syslog adm 0 Mar 17 18:34 /var/log/syslog
2. To truncate a file to a specific size
The example below will truncate a file to 100 bytes in size.
# touch file.txt # ls -lh file.txt -rw-r--r-- 1 root root 0 Mar 17 18:39 file.txt # truncate -s 100 file.txt
Now check the file size:
# ls -lh file.txt
-rw-r--r-- 1 root root 100 Mar 17 18:40 file.txt
To truncate a file to 100 KB:
# truncate -s 100K file.txt # ls -lh file.txt -rw-r--r-- 1 root root 100K Mar 17 18:41 file.txt
The M, G, T, P, E, Z, and Y may be used in place of "K" as required.
3. Extend file size with truncate
You can as well extend the size of the file from current to the desired state. Use the option -s with a + in size.
$ touch file.txt $ truncate -s 100K file.txt $ ls -lh file.txt -rw-r--r-- 1 jmutai wheel 100K Mar 18 13:12 file.txt $ truncate -s +200K file.txt $ ls -lh file.txt -rw-r--r-- 1 jmutai wheel 300K Mar 18 13:12 file.txt
This will extend the file size from 100K to 300K by adding extra 200K.
4. Reduce file size with truncate
Let's say you have a 500K file and would like to shrink it to 250K. You'll use the -s option with a - in size specified. E.g
$ ls -lh file.txt -rw-r--r-- 1 jmutai wheel 500K Mar 18 13:15 file.txt $ truncate -s -250K file.txt $ ls -lh file.txt -rw-r--r-- 1 jmutai wheel 250K Mar 18 13:15 file.txt
You can see the current size changed to 250K.
This tutorial, hope helped you to understand Linux truncate command to alter file to specified size. Please share your suggestion and thoughts on the below comment section.