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