📑

【Linux】ディレクトリをzip化する方法

2023/12/02に公開

zipコマンドを使用してディレクトリを圧縮するには、「-r」オプションを使います。

-r オプションは再帰的(Recursive)を意味し、ディレクトリ内のすべてのファイルとサブディレクトリも含めて圧縮します。

「-r」オプションの使用例

/tmp/test01ディレクトリを圧縮し、カレントディレクトリにtest.zipという名のzipファイルを作成

[oracle@db_ora test01]$ zip -r test.zip /tmp/test01
  adding: tmp/test01/ (stored 0%)
  adding: tmp/test01/test_file_01 (stored 0%)
  adding: tmp/test01/test_file_02 (stored 0%)
  adding: tmp/test01/test_file_03 (stored 0%)
[oracle@db_ora test01]$

/tmp/test01ディレクトリを圧縮し、/tmp/test99ディレクトリにtest.zipという名のzipファイルを作成

[oracle@db_ora test01]$ zip -r /tmp/test99/test.zip /tmp/test01
updating: tmp/test01/ (stored 0%)
updating: tmp/test01/tmp/ (stored 0%)
updating: tmp/test01/tmp/test01/ (stored 0%)
updating: tmp/test01/tmp/test01/test_file_01 (stored 0%)
updating: tmp/test01/tmp/test01/test_file_02 (stored 0%)
updating: tmp/test01/tmp/test01/test_file_03 (stored 0%)
[oracle@db_ora test01]$

おまけ集

せっかくzipを学ぶなら以下の事項も合わせて知っておきましょう。

1. zipの際の出力を簡潔にする⇒「-q」オプション

もしディレクトリ内に多くのファイルがあり、出力を簡潔にしたい場合は、併せて「-q」オプションを使用するのもありです。

[oracle@db_ora test01]$ zip -rq test.zip /tmp/test01
[oracle@db_ora test01]$

2. zipファイルの中身を確認する⇒「zipinfo」コマンド

[oracle@db_ora test01]$ zipinfo test.zip
Archive:  test.zip
Zip file size: 694 bytes, number of entries: 4
drwxr-xr-x  3.0 unx        0 bx stor 24-Mar-22 23:48 tmp/test01/
-rw-r--r--  3.0 unx        0 bx stor 24-Mar-22 23:47 tmp/test01/test_file_01
-rw-r--r--  3.0 unx        0 bx stor 24-Mar-22 23:47 tmp/test01/test_file_02
-rw-r--r--  3.0 unx        0 bx stor 24-Mar-22 23:47 tmp/test01/test_file_03
4 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%
[oracle@db_ora test01]$

Discussion