[Linux] Bundle and Encrypt Files with the zip Command

目次

Overview

The zip command is used to compress and archive files into the ZIP format. This format is highly compatible and is the standard for exchanging data with Windows environments. It allows you to combine multiple files into one while keeping the directory structure. You can also add passwords for basic security.

Specifications (Arguments and Options)

Syntax

zip [options] [zip_filename] [target_files...]

Main Arguments and Options

OptionDescription
-rTraverses directories recursively to include all subfolders and files.
-eEncrypts files and requires a password to extract them.
-P [password]Specifies a password directly on the command line (not recommended as it stays in history).
-x [file]Excludes specific files from the compression.
-@Reads a list of filenames from standard input for compression.
-uUpdates an existing ZIP file or adds new files to it.
-dDeletes a specified file from an existing ZIP archive.
-mDeletes the original files after compression is finished (Move).
-qSuppresses output messages (Quiet mode).
-cAdds a one-line comment to each individual file.
-zAdds a comment to the entire ZIP archive.

Basic Usage

This example compresses an entire directory (e.g., project_docs) into a file named docs_archive.zip. You must use the -r option to maintain the internal folder structure.

Command

# Compress a directory recursively
zip -r docs_archive.zip project_docs/

Execution Result

  adding: project_docs/ (stored 0%)
  adding: project_docs/spec.pdf (deflated 12%)
  adding: project_docs/readme.txt (deflated 45%)
  adding: project_docs/images/logo.png (stored 0%)

Practical Commands

Excluding Specific Files

If you want to create a package but exclude unnecessary items like log files or Git folders, use the -x option.

# Compress my_app while excluding the .git directory and .log files
zip -r release_package.zip my_app/ -x "*.git*" "*.log"

Compressing Results from the find Command

To compress files based on complex conditions (like size or date), use the find command to create a list and pass it to zip using the -@ option.

# Search for .csv files updated within the last 7 days and compress them
find ./data -name "*.csv" -mtime -7 | zip -@ recent_data.zip

Protecting Archives with a Password

When you use the -e option, the system will ask you to enter a password in an interactive prompt.

# Encrypt and compress all text files
zip -e secret_notes.zip *.txt

Execution Example:

Enter password: 
Verify password: 
  adding: memo.txt (deflated 21%)
  adding: todo.txt (deflated 15%)

Note: When extracting with unzip secret_notes.zip, you will be prompted for the password.

Customization Points

  • Adding an Archive Comment (-z): You can add a description or version info to the ZIP file itself.Bashzip -z backup_v1.zip # An input prompt appears. Type your comment and finish with Ctrl+D. You can check the comment later using unzip -l.
  • Adding Individual File Comments (-c): Use this to add a note to each file stored inside the archive.Bashzip -c archive.zip file1.txt

Important Notes

  • Forgetting the -r Option: If you run zip archive.zip directory/ without -r, the command might only create a ZIP file containing an empty folder. Always include -r for directories.
  • Absolute Paths: If you use an absolute path like /var/log/syslog, the full directory structure will be recreated when extracted. This might lead to files being placed in unexpected locations. It is safer to move (cd) into the target directory and use relative paths.
  • Encryption Security: The standard encryption used by the zip command (ZipCrypto) is relatively weak by modern standards. For high security, consider using the gpg command or 7z with AES-256 encryption.

Applications

Maintaining Existing ZIP Files (Add or Delete)

You can edit the contents of a ZIP file directly without extracting it first.

# Add or update new_file.txt in an existing archive.zip
zip -u archive.zip new_file.txt

# Delete old_file.txt from an existing archive.zip
zip -d archive.zip old_file.txt

Summary

The zip command is the best tool for sharing files across different operating systems like Windows and macOS with minimal issues. By mastering the recursive -r and exclusion -x options, you can create clean packages for distribution or backups.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次