The Truncate command is commonly used to shrink or expand a file to a specified size. If the file is larger than the specified size, additional data will be lost. If the file is short, it is extended, and the extension reads zero bytes. System environment Centos7

The truncate command is normally installed on the operating system. This command is included in the Coreutils installation package. If not installed, you can use the following command to install it:

[root@localhost ~]# yum provides truncate

Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Loading mirror speeds from cached hostfile

  • base: mirrors.huaweicloud.com
  • extras: mirrors.aliyun.com
  • updates: mirrors.aliyun.com

Coreutils -8.22-24.el7.x86_64: A Set of Basic GNU Tools Commonly Used in Shell Scripts Repo: Base Matched From: Filename : /usr/bin/truncate

Coreutils -8.22-24.el7.x86_64: A Set of Basic GNU Tools Commonly Used in Shell Scripts Repo: @Anaconda Matched From: Filename : You can see that truncate is provided by the Coreutils installation package. Install the Coreutils installation package below:

[root@localhost ~]# yum -y Install Coreutils Truncate

-c, –no-create –no file created — o, –io-blocks –> values the size as the number of blocks, Instead of bytes -r, –reference=RFILE –> reference to the specified file size -s, –size= size –> set the file size by the specified byte use truncate to clear the contents of the file. This is useful for clearing log files. The truncate procedure removes essentially the entire contents of the file. It does not delete the file itself, but keeps it on disk as a zero-byte file. For example, let’s use truncate to clear /var/log/yum.log to 0 bytes.

[root@localhost ~]# du -sh /var/log/yum.log 12K /var/log/yum.log [root@localhost ~]# truncate -s 0 /var/log/yum.log Looking at the file again, I find that the bytes are now 0. The view content is empty.

[root@localhost ~]# -sh /var/log/yum.log 0 /var/log/yum.log []# cat /var/log/yum.log []# cat /var/log/yum.log How to use Truncate in Linux The Truncate command preserves file ownership. You can use the ll-h command to confirm:

[root@localhost ~]# ll-h /var/log/yum.log-rw root root 0 Nov 4 18:39 /var/log/yum.log How to use Truncate in Linux How do I use the Truncate command in Linux

Set the file to the specified size using truncate The following example fills the file to 10K bytes.

[root@localhost ~]# touch file.txt [root@localhost ~]# ll -h file.txt -rw-r–r–. 1 root root 0 Nov 4 18:43 file.txt [root@localhost ~]# truncate -s 10k file.txt [root@localhost ~]# ll -h file.txt -rw-r–r–. 1 root root 10K Nov 4 18:43 File.txt units are K, M, G, T, P, E, Z, Y.

Extending the file size using truncate also allows you to expand the file size from the current to the desired state. Using the -s option, add a + in front of the number

[root@localhost ~]# cat /etc/passwd > file.txt [root@localhost ~]# ll -h file.txt -rw-r–r–. 1 root root 1009 Nov 4 18:47 file.txt [root@localhost ~]# truncate -s +200k file.txt [root@localhost ~]# ll -h file.txt -rw-r–r–. 1 root root How to use the Truncate command in Linux This will expand the file size from 1K to 201K by adding an additional 200K.

Suppose you have a 500K file and want to reduce it to 250K. The -s option will be used, prefacing the number with a –

[root@localhost ~]# touch file.txt [open ~]# truncate-s 500k file.txt [open ~]# ll-h total 4.0k -rw——-. 1 root root 1.3k Dec 29 2019 anaconda-ks.cfg-rw-r –r– 1 root root 500K Nov 5 08:36 file.txt drwxr-xr-x.5 Root root 107 Nov 4 18:22 test [root@localhost ~]# truncate-s-250k file.txt [c ~]# ll-h total 4.0k -rw——-. 1 root root 1.3k Dec 29 2019 anaconda-ks.cfg-rw-r –r– 1 root root 250K Nov 5 08:36 file.txt drwxr-xr-x.5 The Truncate command on Linux can be seen as the current size has changed to 250K.

The Truncate command is commonly used to shrink or expand a file to a specified size. If the file is larger than the specified size, additional data will be lost. If the file is short, it is extended, and the extension reads zero bytes.