truncate命令的使用

概述

        truncate命令常用语将文件缩小或扩展到指定的大小,如果制定大小小于文件原来的大小,则会丢失额外的数据。如果制定大小大于源文件大小则会扩展此文件

安装

        系统一般会安装truncate命令,这个命令在coreutils安装包里,如果没有可以用下面命令安装

[root@localhost test]# yum install coreutils

truncate的常用选项

        

  -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

SIZE is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000). 
大小位数字,可以用的单位有K, M, G, T, P, E, Z, Y

用法

1.清除文件内容

[root@localhost test]# echo /var/log/messages > important.log
[root@localhost test]# ls
important.log  test.img
[root@localhost test]# du -sh important.log 
4.0K	important.log
[root@localhost test]# truncate -s 0 important.log 
[root@localhost test]# du -sh important.log 
0	important.log

truncate命令保留文件原来所有权

[root@localhost test]# ll -h important.log 
-rw-r--r--. 1 root root 0 Oct 16 16:11 important.log

2.扩展或者缩小文件

可以使用+,-字符来在原来的基础上扩展或缩小文件大小

[root@localhost test]# ll -h
total 200M
-rw-r--r--. 1 root root    0 Oct 16 16:11 important.log
-rw-r--r--. 1 root root 200M Oct 16 15:45 test.img
[root@localhost test]# truncate -s +100M test.img 
[root@localhost test]# ll -h
total 200M
-rw-r--r--. 1 root root    0 Oct 16 16:11 important.log
-rw-r--r--. 1 root root 300M Oct 16 16:18 test.img
[root@localhost test]# 

亦可以直接制定大小

[root@localhost test]# ll -h
total 96M
-rw-r--r--. 1 root root  50M Oct 16 16:34 important.log
-rw-r--r--. 1 root root 600M Oct 16 16:31 test.img
[root@localhost test]# truncate -s 300M test.img 
[root@localhost test]# ll -h
total 96M
-rw-r--r--. 1 root root  50M Oct 16 16:34 important.log
-rw-r--r--. 1 root root 300M Oct 16 16:35 test.img
[root@localhost test]# 

3.这个命令可以裁剪做好的系统镜像img文件

可以通过fdisk -l test.img 查看镜像分区

再用truncate命令进行镜像裁剪

fdisk -l test.img
Disk test.img: 29.8 GiB, 31937527808 bytes, 62377984 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000d89e6
 
Device      Boot   Start      End  Sectors  Size Id Type
OMP120.img1 *       2048  1026047  1024000  500M 83 Linux
OMP120.img2      1026048 59691007 58664960   28G 8e Linux LVM

一个sectors是512k,分区表尾部规划的空间结尾是59691007,那么切除偏移是

truncate --size=$[(59691007+1)*512] test.img

综述

        Truncate命令通常用于将文件缩小或扩展到指定的大小。如果文件大于指定的大小,则会丢失额外的数据。如果文件较短,则会对其进行扩展,并且扩展部分的读数为零字节。

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值