Bash Index : Z - The 'Z' Bash commands : description, flags and examples

mail

zip

Usage

Package and compress (archive) files

Example

archive files

  • zip myArchive fileToZip1 fileToZip2
    .zip is automatically appended to the archive name, except if it already has a .
  • zip myArchive.zip fileToZip1 fileToZip2

list the contents of an archive (source)

  • unzip -l archive.zip
  • zipinfo -1 archive.zip
  • less archive.zip

get the name of the top directory within an archive (see also : awk-based solution) :

  • to give some context :
    less myArchive.zip | head -6
    Archive:  myArchive.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
           0  Stored        0   0% 2021-04-21 12:30 00000000  archiveTopDirectory/		what I'm looking for
           0  Stored        0   0% 2020-11-23 10:36 00000000  archiveTopDirectory/dir1/
           0  Stored        0   0% 2020-11-23 10:36 00000000  archiveTopDirectory/dir1/dir2/
  • solution :
    zipinfo -1 myArchive.zip | head -1
    archiveTopDirectory/

extract multiple .zip files (sources : 1, 2)

unzip archive1.zip archive2.zip
Archive:  archive1.zip
caution: filename not matched:  archive2.zip
This happens because the command above is interpreted as extract archive2 from archive archive1, which doesn't exist.
To extract both archives :
  • unzip '*zip'
  • unzip archive1.zip && unzip archive2.zip
  • for zipFile in archive1.zip archive2.zip do; unzip "$zipFile"; done