Trying to zip/unzip the unix command:

  • you can simply use the zip and unzip commands like,

To compress:

[pastacode lang=”bash” manual=”zip%20squash.zip%20file1%20file2%20file3%0A” message=”bash code” highlight=”” provider=”manual”/]

To zip a directory

[pastacode lang=”bash” manual=”zip%20-r%20squash.zip%20dir1%0A” message=”bash code” highlight=”” provider=”manual”/]

To uncompress:

[pastacode lang=”bash” manual=”unzip%20squash.zip%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

this unzips it in your current working directory.

  • Use tar to create an uncompressed archive and either gzip or bzip2 to compress that archive.
  • The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression.
  • If you don’t have zip and unzip packages installed and you have java, you can use jar to unzip:
[pastacode lang=”bash” manual=”jar%20-xf%20file.zip%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • You can zip files up (in compressed format) with the GNU tar program:

Example:

[pastacode lang=”bash” manual=”tar%20-zcvf%20myfile.tgz%20″ message=”bash code” highlight=”” provider=”manual”/]

where

  • -c means “create”
  • -v means “verbose” (sometimes bothersome and slowing down…)
  • -z means “use (GNU)zip”
  • -f XYZ declares the name of the output file.
  • To unzip that file, use:
[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20tar%20-zxvf%20myfile.tgz%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • That you have a tar capable of doing the compression as well as combining of files into one.
  • If not, you can just use tar cvf followed by gzip (again, if available) for compression and gunzip followed by tar xvf.

where

  • x means “eXtract”
  • -v means “verbose” (sometimes bothersome and slowing down…)
  • -z means “use (GNU)zip”
  • -f XYZ declares the name of the output file.

Zip Command Examples in Unix / Linux

  • zip is used to compress the files to reduce file size and also used as file package utility.
  • zip is available in many operating systems like unix, linux, windows etc.
  • If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.
  • The syntax of zip command is:
[pastacode lang=”bash” manual=”Zip%20%5Boptions%5D%20zipfile%20files_list%20″ message=”bash code” highlight=”” provider=”manual”/]

 Zip Command Examples:

  • The files in my current directory are listed below:
[pastacode lang=”bash” manual=”docs%2Flinux.pdf%20docs%2Foracle.pdf%20docs%2Funix.pdf%20linux-virtual-server.bat%20unix-server.dat%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • Here docs is a directory which contains the files linux.pdf, unix.pdf and oracle.pdf.

The options of zip command are:

  • d : Removes the file from the zip archive
  • -u : Updates the file in the zip archive
  • -m : Deletes the original files after zipping.
  • -r : Recursively zips the files in a directory
  • -x : Exclude the files in creating the zip
  • -v : verbose mode
  • -1 : Compresses the files faster
  • -9 : Compresses the files better
  • -f : freshen only changed files.

zipfile     : creates the zip file with name as zipfile.zip
files_list : list of files to be zipped.

Examples for how to use zip command ,

1. Creating a zip file

The zip command in unix or linux system creates an archive with the specified files. This is shown below:

[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%3Ezip%20archive%20linux-virtual-server.bat%20unix-server.dat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20linux-virtual-server.bat%20(deflated%2080%25)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20unix-server.dat%20(deflated%2080%25)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3E%20ls%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20wikitechy.zip%20docs%20linux-virtual-server.bat%20unix-server.dat%20%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • The above command creates the zip file with name wikitechy.zip

2. Extracting files from zip

To extract files from the zip, use the unzip command in unix system. This is shown below:

[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%3Eunzip%20archive.zip%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Archive%3A%20archive.zip%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20inflating%3A%20linux-virtual-server.bat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20inflating%3A%20unix-server.dat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3E%20ls%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20archive.zip%20linux-virtual-server.bat%20unix-server.dat%20%0A” message=”bash code” highlight=”” provider=”manual”/]

3. Removing file from a zip file

  • After creating a zip file, you can remove a file from the archive using the -d option.
  • To remove the file unix-server.dat from the archive, run the below zip command:
[pastacode lang=”bash” manual=”zip%20-d%20archive.zip%20unix-server.dat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20deleting%3A%20unix-server.dat%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%3E%20unzip%20archive.zip%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Archive%3A%20archive.zip%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20inflating%3A%20linux-virtual-server.bat%0A” message=”bash code” highlight=”” provider=”manual”/]

4. Update existing zip file

  • You can update the files in already created zip file. If any of the files are modified after zipping, you can fresh the zip file with only those modified files using the -f option.
[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zip%20-f%20archive.zip%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20freshening%3A%20linux-virtual-server.bat%20(stored%200%25)%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • Another way is using the -u option. This option can be used to update the specified list of files or add new files to the existing zip file.
[pastacode lang=”bash” manual=”zip%20-u%20archive.zip%20linux-virtual-server.bat%20temp%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20updating%3A%20linux-virtual-server.bat%20(deflated%2079%25)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20temp%20(stored%200%25)%20%0A” message=”bash code” highlight=”” provider=”manual”/]

5. Recursively zip files in directory.

  • To zip a directory recursively, use the -r option with the zip command. This example is shown below
[pastacode lang=”bash” manual=”%3Ezip%20-r%20dir_archive%20docs%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20docs%2F%20(stored%200%25)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20docs%2Funix.pdf%20(stored%200%25)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20docs%2Foracle.pdf%20(stored%200%25)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20docs%2Flinux.pdf%20(stored%200%25)%20%0A%0A” message=”bash code” highlight=”” provider=”manual”/] [ad type=”banner”]

6. Excluding files in zipping

  • Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.
[pastacode lang=”bash” manual=”zip%20exclude_archive%20*%20-x%20linux-virtual-server.bat%20%0A” message=”bash code” highlight=”” provider=”manual”/]
  • The above command zips all the files in the current directory except the file linux-virtual-server.bat

7. Faster compressing

  • You can compress the files very fast using the -1 option with zip command. An example is shown below with and without using fast compression.
[pastacode lang=”bash” manual=”%3Ezip%20-1%20fast_archive%20linux-virtual-server.bat%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20linux-virtual-server.bat%20(deflated%2079%25)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ezip%20normal_archive%20linux-virtual-server.bat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20linux-virtual-server.bat%20(deflated%2080%25)%0A%0A” message=”bash code” highlight=”” provider=”manual”/]
  • If you use fast compression, the archive file created will occupy more space (size) when compared to normal compression.

8. Better compression.

  • To reduce more amount of size the files occupied, you can use the -9 option with the zip command. This gives a better compression.
[pastacode lang=”bash” manual=”%3Ezip%20-9%20better_archive%20linux-virtual-server.bat%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adding%3A%20linux-virtual-server.bat%20(deflated%2081%25)%20%0A” message=”bash code” highlight=”” provider=”manual”/]

Categorized in: