Linux truncate的使用方法介绍

Linux truncate的使用方法介绍


参考资料:https://www.fengbohello.top/archives/linux-truncate


本命令缩减或扩充指定文件的大小为指定值。参数所指定的文件如果不存在,那么该命令会创建这个文件。

如果一个文件的大小比参数指定的大,那么超出的部分就会被丢弃。如果一个文件比参数指定的小,那么文件会被扩充,并且被扩充的部分(空洞)在被读取的时候是字节0。

命令格式:

truncate 选项 文件列表


命令详解:对于长选项来说必须的参数,对于短选项来说也是必须的。

-c, --no-create
    不创建任何文件

-o, --io-blocks
    把参数指定的大小视为 I/O 块,而不是视为字节

-r, --reference=FILE
    使用文件 FILE 的大小作为参考大小

-s, --size=SIZE
    使用 SIZE 指定文件的大小

--help display this help and exit
    显示这个帮助信息

--version
    输出版本信息,然后退出

SIZE 参数可以是(或者是一个整数后面跟着任意的)下面的选项:KB 1000, K 1024,
    MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

SIZE 之前也可以加上下面的特性:
    '+' 增加 SIZE,
    '-' 减少 SIZE,
    '<'最大为 SIZE,
    '>'最小为 SIZE,
    '/'以SIZE为除数,向下取整,
    '%'以SIZE为除数,向上取整。

注意:-r 和 -s 选项是互斥的。

一些具体操作记录:

[root@my1-222 ~]# truncate -c --size 2000m x.dbf
# 源文件test.db和目标文件test.db.bak
[root@my1-222 ~]# ll -th /root/test.db
-rw-r--r--. 1 root root 12G May 24 01:26 /root/test.db
[root@my1-222 ~]# ll -th /root/test.db.bak
-rw-r--r--. 1 root root 0 May 24 17:51 /root/test.db.bak
[root@my1-222 ~]#

# 利用truncate瞬间制造大小相同的文件
[root@my1-222 ~]# truncate -r test.db test.db.bak
[root@my1-222 ~]# ll -th /root/test.db.bak
-rw-r--r--. 1 root root 12G May 24 17:56 /root/test.db.bak
[root@my1-222 ~]#

[root@my1-222 ~]# truncate  --size 10G test.db.bak
[root@my1-222 ~]# ll -th /root/test.db.bak
-rw-r--r--. 1 root root 10G May 24 18:01 /root/test.db.bak
[root@my1-222 ~]# 
原文地址:https://www.cnblogs.com/bjx2020/p/9081334.html