首頁·終端機命令

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