Bash 速習チュートリアル

Bash zip コマンド

1. zip コマンドの使用方法

zip コマンドは、ファイルを ZIP アーカイブにパッケージングおよび圧縮するために使用されます。

1.1 基本的な使い方

ZIP アーカイブを作成するには、zip archive.zip file1 file2 という構文を使用します。

実行例:

zip archive.zip file1 file2
adding: file1 (stored 0%)
adding: file2 (stored 0%)

2. ZIP オプションの概要

zip コマンドで使用される主なオプションは以下の通りです:

  • -r - ディレクトリを再帰的に圧縮する
  • -u - アーカイブ内のファイルが新しい場合のみ更新する
  • -d - アーカイブからファイルを削除する
  • -e - ZIP アーカイブの内容を暗号化する
  • -x - 特定のファイルを圧縮対象から除外する

2.1 オプション:-r (Recursive)

-r オプションを使用すると、ディレクトリとその配下にあるコンテンツを再帰的に圧縮できます。

実行例:再帰的圧縮

zip -r archive.zip folder/
adding: folder/ (stored 0%)
adding: folder/file1 (stored 0%)
adding: folder/file2 (stored 0%)
adding: folder/subfolder/ (stored 0%)
adding: folder/subfolder/file3 (stored 0%)

2.2 オプション:-u (Update)

-u オプションは、既存のアーカイブ内のファイルよりも新しいファイルがある場合にのみ、ファイルを更新(または追加)します。

実行例:アーカイブの更新

zip -u archive.zip file1 file2
updating: file1 (deflated 60%)
updating: file2 (deflated 60%)

2.3 オプション:-d (Delete)

-d オプションは、アーカイブ内から指定したファイルを削除します。

実行例:アーカイブからの削除

zip -d archive.zip file1
deleting: file1

2.4 オプション:-e (Encrypt)

-e オプションは ZIP アーカイブの内容を暗号化し、解凍(unzip)時にパスワードを要求するように設定します。

実行例:アーカイブの暗号化

zip -e archive.zip file1 file2
Enter password:
Verify password:

2.5 オプション:-x (Exclude)

-x オプションを使用すると、特定のファイルをアーカイブへの追加対象から除外できます。

実行例:ファイルの除外

zip archive.zip file1 file2 -x file2
adding: file1 (stored 0%)