tar
圧縮と転送
書き方
tar -czf <archive.tar.gz> <path>ファイルを一つの束にまとめるだけで、それ自体は圧縮しません。縮めるのは `-z`(gzip)・`-j`(bzip2)・`-J`(xz) の役目で、作る `-c`・取り出す `-x`・中身を見る `-t` のうち一つだけを先に置き、古い書き方では `-f` を最後のオプションにして、その直後に書庫名を書きます。
よく使うオプション
| オプション | 意味 |
|---|---|
| -c / -x / -t | Create / extract / list. Exactly one of the three, and it comes first. |
| -f <file> | The archive name, and it must be the last flag before that name. |
| -z / -j / -J | gzip / bzip2 / xz. GNU tar detects the format when extracting, so -z is optional with -x. |
| -v | Print the file names as they go past. |
| -C <dir> | Change into that directory first. Position in the command line matters. |
| --exclude='node_modules' | Skip matching paths. Write it before the source paths. |
| --strip-components=1 | Drop the top-level directory when extracting. |
| --zstd | Use zstd instead of gzip (GNU tar 1.31 and newer). |
例
tar -czf site.tar.gz -C /var/www .Archive the contents of a directory without the leading path.
tar -xzf site.tar.gz -C /tmp/restoreExtract into a directory that already exists.
tar -tzf site.tar.gz | headLook inside before extracting, so nothing lands in $PWD by surprise.
まとめることと縮めることは別の仕事です。tar がまとめ、gzip が縮め、.tar.gz はその両方です。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. tar は何をしますか。
ファイルを一つの束にまとめるだけで、それ自体は圧縮しません。縮めるのは `-z`(gzip)・`-j`(bzip2)・`-J`(xz) の役目で、作る `-c`・取り出す `-x`・中身を見る `-t` のうち一つだけを先に置き、古い書き方では `-f` を最後のオプションにして、その直後に書庫名を書きます。
Q. どう打ちますか。
tar -czf <archive.tar.gz> <path> — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは8個です。全部は man tar にあります。このコマンドは圧縮と転送の仲間です。
関連するコマンド
man ページ: man tar