Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the truncate command in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use the truncate command in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The truncate command reduces or expands the size of the specified file to the specified value. If the file specified by the parameter does not exist, the command creates the file. If the size of a file is larger than that specified by the parameter, the excess will be discarded. If a file is smaller than the parameter specified, the file is expanded and the expanded part (hole) is byte 0 when read.

System environment

Centos7

Installation

Usually, the operating system installs the truncate command, which is in the coreutils installation package. If it is not installed, you can install it using the following command:

[root@localhost] # yum provides truncateLoaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-managerThis 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.comcoreutils-8.22-24.el7.x86_64: A set of basic GNU tools commonly used in shell scriptsRepo: baseMatched from:Filename: / usr/bin/truncatecoreutils-8.22-24.el7.x86_64: A set of basic GNU tools commonly used in shell scriptsRepo: @ anacondaMatched from:Filename: / usr/bin/truncate

You can see that truncate is provided by the coreutils installation package, so install the coreutils installation package below:

[root@localhost ~] # yum-y install coreutilstruncate common options

Here are some common options for truncate:

-c,-- no-create-- > do not create any file-o,-- io-blocks-- > treats the size as the number of storage blocks, not bytes-r,-- reference=RFILE-- > refers to the specified file size-s,-- size=SIZE-- > clears the file contents using truncate according to the specified byte setting file size

This is useful for clearing log files. The truncate process basically deletes all the 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.log12K / var/log/yum.log [root@localhost ~] # truncate-s 0 / var/log/yum.log

Look at the file again and find that the byte is 0. The content of the check is empty.

[root@localhost ~] # du-sh / var/log/yum.log0 / var/log/yum.log [root@localhost ~] # cat / var/log/yum.log

The truncate command retains ownership of the file. You can use the ll-h command to confirm:

[root@localhost] # ll-h / var/log/yum.log-rw-. 1 root root 0 Nov 4 18:39 / var/log/yum.log uses truncate to set the file to the specified size

The following example populates 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

The units are K, M, G, T, P, E, Z, Y.

Use truncate to expand file size

You can also expand the size of the file from the current to the desired state. Using the-s option, precede the number with +

[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 201K Nov 4 18:47 file.txt

By adding an additional 200K, this expands the file size from 1K to 201K.

Use truncate to reduce file size

Suppose you have a 500K file and want to narrow it down to 250K. The-s option will be used, preceded by-

[root@localhost] # touch file.txt [root@localhost] # truncate-s 500k file.txt [root@localhost] # ll-htotal 4.0K Murray. 1 root root 1.3K Dec 29 2019 anaconda-ks.cfg-rw-r--r--. 1 root root 500K Nov 5 08:36 file.txtdrwxr-xr-x. 5 root root 107 Nov 4 18:22 test [root@localhost] # truncate-s-250k file.txt [root@localhost] # ll-htotal 4.0K Murray RWMI. 1 root root 1.3K Dec 29 2019 anaconda-ks.cfg-rw-r--r--. 1 root root 250K Nov 5 08:36 file.txtdrwxr-xr-x. 5 root root 107 Nov 4 18:22 test

You can see that the current size changes to 250K.

Thank you for reading! This is the end of the article on "how to use truncate commands in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report