In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use Truncate commands in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Truncate commands in Linux" this article.
The Truncate command is typically used to shrink or expand a file to a specified size. If the file is larger than the specified size, additional data is lost. If the file is short, it is extended and the reading of the extension is zero bytes.
System environment
Linux version: 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 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: / 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 coreutils
Common options for truncate
Here are some common options for truncate:
-c,-- no-create-- > do not create any files-o,-- io-blocks-- > treat the size as the number of storage blocks, not bytes-r,-- reference=RFILE-- > refer to the specified file size-s,-- size=SIZE-- > set the file size by the specified bytes
Clear the contents of the file using truncate
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.log 12K / 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.log 0 / 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
Use 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-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 [root@localhost ~] # 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 root root 107 Nov 4 18:22 test
You can see that the current size changes to 250K.
The above is all the contents of the article "how to use Truncate commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.