Linux zip command

The zip command in Linux is used to create compressed zip archives of files and directories. It allows you to combine multiple files and directories into a single compressed archive, making it easier to store, transfer, and share files. The zip command is a popular tool for creating and extracting zip files in Linux.

Here’s an overview of how to use the zip command:

  1. Basic Usage:
    To create a zip archive, use the following syntax:
   zip <archive_name.zip> <file1> <file2> <directory1> <directory2> ...
  1. Creating a Zip Archive:
    To create a zip archive, specify the name of the archive followed by the files and directories you want to include. For example:
   zip archive.zip file1.txt file2.txt directory1 directory2
  1. Including Subdirectories:
    By default, the zip command includes the contents of subdirectories when you specify a directory. To include the subdirectories themselves as well, use the -r option (recursive). For example:
   zip -r archive.zip directory
  1. Excluding Files and Directories:
    You can exclude specific files and directories from the zip archive using the -x option followed by patterns. Patterns can include wildcards (*) and can be used to exclude multiple files or directories. For example:
   zip -r archive.zip directory -x "*.log" "tmp/*"
  1. Adding Files to an Existing Archive:
    To add files to an existing zip archive, use the -u option (update). For example:
   zip -u archive.zip new_file.txt
  1. Viewing the Contents of a Zip Archive:
    To list the contents of a zip archive without extracting them, use the -sf option (short format). For example:
   zip -sf archive.zip
  1. Extracting Files from a Zip Archive:
    To extract the contents of a zip archive, use the unzip command followed by the name of the zip archive. For example:
   unzip archive.zip

8.Examples:

  • Create a zip archive named files.zip containing file1.txt and file2.txt:
zip files.zip file1.txt file2.txt

Create a zip archive named archive.zip containing the contents of directory1 and directory2:

zip -r archive.zip directory1 directory2

Create a zip archive named archive.zip, excluding all .log files and the tmp directory:

zip -r archive.zip directory -x "*.log" "tmp/*"

Add a new file named new_file.txt to an existing zip archive named archive.zip:

zip -u archive.zip new_file.txt

Extract the contents of a zip archive named archive.zip:

unzip archive.zip

The zip command provides a convenient way to create compressed zip archives in Linux. You can customize the compression level, include or exclude specific files, and perform various operations on zip archives. For more information about the zip command and its options, you can refer to the manual page by typing man zip in your terminal.