In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how to use the truncate command in Linux, I hope you have gained something after reading this article, let's discuss it together!
The truncate command allows you to shrink or expand a file to a given size. You can specify the file size using this command and the-s option.
system environment
Centos7
installation
Usually the operating system will install the truncate command, which is installed in the coreutils installation package. If it is not installed, you can install it by 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
As you can see truncate is provided by the coreutils installation package, install the coreutils installation package below:
[root@localhost ~]# yum -y install coreutilstruncate Common Options
Here are the common options for truncate:
-c, --no-create --> don't create any files-o, --io-blocks --> treat size as number of storage blocks, not bytes-r, --reference=RFILE --> reference-specified file size-s, --size=SIZE --> set file size by specified bytes use truncate to clear file contents
This is useful for cleaning log files. The truncate process basically deletes everything from 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
Looking at the file again, I found that the byte is 0. View content 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 file ownership. You can confirm this with the ll -h command:
[root@localhost ~]# ll -h /var/log/yum.log-rw-------. 1 root root 0 Nov 4 18:39 /var/log/yum.log Set file to specified size using truncate
The following example fills a 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.
Extending file size with truncate
You can also expand the file size from its current state to the desired state. Use the-s option to precede the number with a +
[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 extra 200K, this expands the file size from 1K to 201K.
Reduce file size with truncate
Suppose you have a 500K file and want to shrink it to 250K. The-s option will be used to precede the number with-
[root@localhost ~]# touch file.txt[root@localhost ~]# truncate -s 500k file.txt [root@localhost ~]# ll -htotal 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.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-rw-------. 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.
After reading this article, I believe you have a certain understanding of "how to use the truncate command in Linux". If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!
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.