首页·终端命令

truncate

文件与目录

用法

truncate -s [size] [file]

把文件长度设成正好那么大:变大只是打个稀疏空洞并不真占空间,变小则不作提醒地丢掉尾部。

常用选项

选项含义
-sSet the length; a leading + or - grows or shrinks instead
-cDo not create the file if it is missing
-rTake the length from another file
-oGNU: read the size as a number of blocks

示例

truncate -s 0 app.log

Empties a log while the program keeps writing to it.

truncate -s 1G disk.img

A sparse 1 GB file, created instantly.

truncate -s -100 data.bin

Cuts the last 100 bytes off.

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. truncate 是做什么的?

把文件长度设成正好那么大:变大只是打个稀疏空洞并不真占空间,变小则不作提醒地丢掉尾部。

Q. 怎么写?

truncate -s [size] [file] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 4 个,完整列表在 man truncate。这条命令属于文件与目录。

相关命令

man 手册: man truncate