truncate
ファイルとディレクトリ
書き方
truncate -s [size] [file]ファイルの長さをちょうどその大きさに合わせます。伸ばすときは穴を空けるだけで領域を確保せず、縮めるときは切れた後ろを警告なしに捨てます。
よく使うオプション
| オプション | 意味 |
|---|---|
| -s | Set the length; a leading + or - grows or shrinks instead |
| -c | Do not create the file if it is missing |
| -r | Take the length from another file |
| -o | GNU: read the size as a number of blocks |
例
truncate -s 0 app.logEmpties a log while the program keeps writing to it.
truncate -s 1G disk.imgA sparse 1 GB file, created instantly.
truncate -s -100 data.binCuts the last 100 bytes off.
移したり消したり探したりするものです。聞き返してくれない命令が混ざっているので、-i を付ける癖が手を守ります。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. truncate は何をしますか。
ファイルの長さをちょうどその大きさに合わせます。伸ばすときは穴を空けるだけで領域を確保せず、縮めるときは切れた後ろを警告なしに捨てます。
Q. どう打ちますか。
truncate -s [size] [file] — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは4個です。全部は man truncate にあります。このコマンドはファイルとディレクトリの仲間です。
関連するコマンド
man ページ: man truncate