How to Zip / Unzip on Linux

Bash Liste Des Attaques Ovh

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 ?

How to

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

Unzip all files except the root directory

When download zip archive, you get in the created archive in the root a single directory with the name of the archive. If you want to skip its creation, you can't do it with the command line unzip. You need to use the bsdtar command line tool.

Example on debian

apt-get install -y libarchive-tools 
bsdtar --strip-components=1 -xvf file.zip

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.

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

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"

Zip a file

zip targetFile.zip /myFile

Unzip in a target directory

unzip file.zip -d myTargetDirectory

Zip only a subset of a files

Under Unix, the option -@ 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 -@

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 -@

where:

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

Support

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





Discover More
Bash Liste Des Attaques Ovh
Bash - Directory

directory management with bash. See To get the space by directory, you can use the du command. It summarize disk usage of each FILE, recursively for directories. With a redirection, you...



Share this page:
Follow us:
Task Runner