Linux - Zip / Unzip

1 - About

The zip manager in Linux

Compression:

  • take advantage of redundancy between files.

split archives:

  • storing a large archive on multiple removable media.
  • split big file ?

3 - How to

3.1 - List

List the content


unzip -l myFile.zip

Example:


/usr/bin/unzip -l p25797429_111190_Linux-x86-64.zip


Archive:  p25797429_111190_Linux-x86-64.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  04-12-17 08:55   25797429/
    68000  04-12-17 08:55   25797429/p21517672_111190_Generic.zip
318682896  04-12-17 08:30   25797429/p25710656_111190_Linux-x86-64.zip
545626254  04-12-17 08:55   25797429/p25818798_111190_Generic.zip
456596466  04-12-17 08:33   25797429/p25710602_111190_Linux-x86-64.zip
182417792  04-12-17 08:55   25797429/p25738050_111190_Generic.zip
   114984  04-15-17 20:38   25797429/README.html
1052740398  04-12-17 08:55   25797429/p25710734_111190_Generic.zip
 91360658  04-12-17 08:55   25797429/p24346370_111190_Generic.zip
1036787935  04-12-17 08:36   25797429/p25796832_111190_Linux-x86-64.zip
 --------                   -------
3684395383                   10 files

3.2 - Unzip all zip files of a directory


/usr/bin/unzip \*.zip


..........
  ..........
  inflating: 13952743/files/common/script_handlers/MDS_handler.py
  creating:  13952743/files/clone/
  creating:  13952743/files/clone/provision/
  inflating: 13952743/files/clone/provision/adf_t2p_registration.xml
  inflating: 13952743/files/clone/provision/adf-t2p-cloner.jar
  inflating: 13952743/README.txt

9 archives were successfully processed.

3.3 - Unzip only a sub-directory of the zip file

The root directory is still preserved 'my/sub/folder', you need to move it. The -j option flatten the whole structure and is not what you want

unzip file.zip 'my/sub/folder' -d /dest/dir

3.4 - Zip a directory recursively


/usr/bin/zip -r targetFile.zip ./targetDir/*

in the background


nohup zip -r file.zip directory/* &

You can monitor it with the command:

jobs

or with this until statement (You need to change the pid)


pid=22018
count=0
while kill -0 $pid 2> /dev/null; do
    count=$(( $count + 1 ))
    echo "${count} - Process is running"
    sleep 10
done
echo "${count} - Process has exited"

3.5 - Zip a file


zip targetFile.zip /myFile

3.6 - Unzip in a target directory


unzip file.zip -d myTargetDirectory

3.7 - Zip only a subset of a files

Under Unix, the option [email protected] file list can be used to powerful effect in conjunction with the find command.

For example, to archive all the C source files in the current directory and its subdirectories:


find . -name "*.[ch]" -print | zip source [email protected]

3.8 - Zip a subset of files selected by attributes (date/name...)

With the find

Example: zip all files modified the last 7 days


find . -mtime -7  -type f -print | zip lastSevenDaysModifiedFiles.zip [email protected]

where:

  • . means start the search from the current directory
  • -mtime -7 means select all files modified from 7 days ago.
  • [email protected] tells zip to read files from the input

4 - Support

4.1 - unzip: cannot find or open

You may get this kind of message:


unzip:  cannot find or open ./p25797429_111190_Linux-x86-64.zip, ./p25797429_111190_Linux-x86-64.zip.zip or ./p25797429_111190_Linux-x86-64.zip.ZIP.

Just try to use another unzip application.

Example:


/usr/bin/unzip


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap