0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

めちゃめちゃでかいtarファイルが送られて困った時の対処法

Last updated at Posted at 2025-04-15

めちゃめちゃでかいtarファイルが送られて困った時の対処法

巨大な .tar ファイル(Tape Archive形式)を受け取ったとき、全部展開すると時間もディスク容量も無駄になってしまうことがあります。
そんなときは、必要なファイルだけをピンポイントで抽出しましょう。


特定のファイルを抽出する基本

たとえば、archive.tar の中から path/to/file.txt だけを取り出したい場合は、次のように実行します:

tar xf archive.tar path/to/file.txt

パスの確認方法

注意点として、指定するパスは tarアーカイブ内の構造に一致している必要 があります。
まずは以下のコマンドでアーカイブの中身を確認しましょう。

tar tf archive.tar

これでファイルの正確なパスが分かります。


抽出先のディレクトリを指定したい場合

-C オプションを使うことで、ファイルの展開先を指定することができます。
例えば、/tmp/extract_here に展開するには次のようにします:

tar xf archive.tar path/to/file.txt -C /tmp/extract_here

※ 指定先ディレクトリ(この例では /tmp/extract_here)は事前に存在している必要があります。


コマンドまとめ

+--------------------------+-----------------------------------------------------------+
| 操作                     | コマンド例                                                |
+--------------------------+-----------------------------------------------------------+
| アーカイブの中身を確認   | tar tf archive.tar                                        |
| 特定ファイルを抽出       | tar xf archive.tar path/to/file.txt                       |
| 展開先を指定して抽出     | tar xf archive.tar path/to/file.txt -C /tmp/extract_here  |
+--------------------------+-----------------------------------------------------------+

おまけ:中身を展開せずに確認したい場合

ファイルを実際に展開せずに中身だけ見たいときは、--to-stdout オプションを使います:

tar xf archive.tar path/to/file.txt --to-stdout | less

このコマンドで、ファイルの内容を標準出力に流し、less でページング表示できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?