Compression (zip), compression zip

Source: Internet
Author: User

Compression (zip), compression zip

By default, these compression tools will delete the source files (except zip files) after compression. By default, only files are compressed, but directories are not compressed (LinkPackage).

    1. Gzip

    2. Bzip2

    3. Zip

    4. GNU tar


    • 1. gzip

    1. Compression

    Gzip is used to compress a common file named ". GZ ". Delete the original file after compression.

    $ gzip

    Multiple files can be compressed at a time. Each file is compressed independently.

    $ gzip *


    . Decompress

    Decompress the file and delete the original file.

    $ gunzip

    In fact, the Command Used for decompression is a script.

    #!/bin/shPATH=${GZIP_BINDIR-'/bin'}:$PATHexec gzip -d "$@"


    1. 3. test the integrity of the compressed file

    $ gzip -t hellogzip: hello: unexpected end of file


    1. 4. View compressed file content

    $ zcat hello.gz


    • 2.bzip2/Xz

    2. 1. Compression

    The original file is also deleted.

    $ bzip2 *

    When an asterisk is used, multiple files are compressed, and each file is compressed independently.


    2. Decompress

    $ bunzip2 *$ bzip2 -d *


    2. 3. Retain the original file

    $ bzip2 -k hello

    Asterisks are not supported.


    2. 4. View compressed file content

    $ bzcat hello.bz2


    • 3. zip

    For compression, the name of the compressed package is zhangfooyun, and the final name of the compressed package is ". Append members. No specific options are required.
    During compression, the system runs in a unix environment and supports regular expression matching :? , *, []. Match a single character, match any number of characters (including null), and match one of them.

    3. 1. compress the Directory

    Directory can be directly compressed

    $ zip mash/


    In this way, the directory can be directly compressed, but if the directory "mash" is full of directory files, the compressed package is empty. Because the directory is compressed ).

    Compress all files under the "zip-dir" Directory (including files in subdirectories and subdirectories), decompress the package, and decompress the package with the "zip-dir" directory ". (Options include "-r" and asterisks "*" at the end.)

    $ zip -r zip-dir/*


    3. 2. compressed files

    Compress all files and directories in the current directory (excluding files in the subdirectories of the current directory ).

    $ zip zip-dir *


    3. specify the location of the compressed package

    Specify to another directory.

    $ zip /test/zip-dir *


    3. 4. Delete the original file after compression

    Create a compressed file named “ and delete the original file named "all ".

    $ cd zip-dir$ mkdir all$ zip -rm all all


    Append the files in the current directory to the compressed file and delete the original file.

    $ zip -rm all *


    . Set a password for the compressed package

    Encrypt the compressed package with the option "-e ".

    $ zip -r -e bin/*Enter password: Verify password:   adding: bin/ (deflated 57%)  adding: bin/ (deflated 52%)  adding: bin/ (deflated 58%)  adding: bin/ (deflated 56%)


    You can specify the encrypted password directly on the command line, and use uppercase "-P ":

    # zip -r -e -P hello bin/*


    . Display the number of compressed files

    After compression is completed, the number of compressed files is displayed.

    $ zip -dc bin/*  0/  4 updating: bin/ (deflated 57%)  1/  3 updating: bin/ (deflated 52%)  2/  2 updating: bin/ (deflated 58%)  3/  1 updating: bin/ (deflated 56%)


    . Remove the directory after compression

    Only compress the files in the specified directory, and does not include any subdirectories.Current Directory (zip-dir). (After decompression, only the directories are invisible to the files)

    $ zip -j zip-dir zip-dir/*


    Instance: Get tomcat logs (The compressed path is long and inconvenient to use.):

    [root@right mag]# zip -j /home/work/tomcat4file/logs/catalina.out   adding: catalina.out (deflated 93%)


    . Change the compression input mode(Compressed files are from pipelines)

    $ find /etc/sysconfig/network-scripts -name "ifcfg*" -print | zip ifcfg -@


    . Append the file to the compressed package

    Append an object to a compressed package.

    [root@right mag]# zip back 1.txt  adding: 1.txt (stored 0%)[root@right mag]# zip back 2.conf  adding: 2.conf (stored 0%)[root@right mag]# zip back 3.xml 4.html 5.sql  adding: 3.xml (stored 0%)  adding: 4.html (stored 0%)  adding: 5.sql (stored 0%)[root@right mag]# unzip -l backArchive:  Length      Date    Time    Name---------  ---------- -----   ----        0  01-11-2014 15:55   1.txt        0  01-11-2014 15:55   2.conf        0  01-11-2014 15:55   3.xml        0  01-11-2014 15:55   4.html        0  01-11-2014 15:55   5.sql---------                     -------        0                     5 files


    3. 10. compress the specified file

    Use the Options "-I" and "-- include" to obtain files in the specified format:

    zip -r foo . -i \*.c

    Only some logs are compressed, for example:

    [root@right mag]# zip -r /home -i \*.out  adding: home/work/tomcat4file/logs/catalina.out (deflated 93%)[root@right mag]# unzip -l Archive:  Length      Date    Time    Name---------  ---------- -----   ----132020334  01-11-2014 15:09   home/work/tomcat4file/logs/catalina.out---------                     -------132020334                     1 file


    R & D personnel often ask for files:

    [root@iZ28srao5ssZ mag]# zip -rj /home/work -i "*"  adding: (deflated 63%)


    . Skip the specified file during compression

    Specify the Options "-x" and "-- exclude ".
    Skip the mobile client program:

    # zip -r /home/work/release/* -x \*.apk


    3.12.Deletes a specified file in a compressed file.

    Specify the name and delete it with the option "-d.

    [root@iZ28srao5ssZ mag]# zip -d home/work*deleting: home/work/release/caiBao/WEB-INF/classes/[work@app47 .donatello]$ zip -d _re*deleting:


    3.13.Check files

    Option "-T"


    # zip -T test of OK




    • Unzip

    3. 1. Test the compressed file

    Check whether there is any error after compression

    $ unzip -t binArchive:    testing: bin/                     OK    testing: bin/   OK    testing: bin/   OKNo errors detected in compressed data of$ unzip -tq binNo errors detected in compressed data of


    . List Files in the compressed package

    $ unzip -l

      List file information in a compressed package in detail

    $ unzip -v


    3. Decompress all files

    Extract all files in zc0000zip

    $ unzip zcwArchive:  inflating:    inflating:


    Remove the directory structure during decompression.

    $ unzip -j bin


    Decompress a specified file

    $ unzip bin \*zcw_V*.shArchive:  inflating: bin/


    • FAQ: Location after decompression

    $ zip /etc/yum.repos.d/CentOS-Base.repo$ unzip -l base.repo.zipArchive:  Length      Date    Time    Name---------  ---------- -----   ----     1926  12-28-2014 14:22   etc/yum.repos.d/CentOS-Base.repo---------                     -------     1926                     1 file


    That is:

    $ cd /data/Test$ unzip /root/$ ls -R etc/
    $ ls /data/Test/etc/yum.repos.d/CentOS-Base.repo/data/Test/etc/yum.repos.d/CentOS-Base.repo


    3. 4. Specify the extract directory

    By default, all files and subdirectories are extracted to the current directory. You can use the "-d" option to specify the directory.

    # unzip -d /home/work/payment_front_pro/release/


    • 4. GNU tar


    Main Operation Mode

    -Cf archive Packaging

    -Tf: view the archive package (the effect is similar to the display style of "ls", but is not separated by blank space, but by line feed)

    -Tvf: view the archive package (the effect is similar to the "ls-l" display style)

    -Xf archive unpacking


    General options

    -C: Specifies the extract directory.

    -V: List processed files in detail

    $ tar -cf demo.tar demo/$ tar -tf demo.tar…… $ tar -xf nari.tar


    4. 1. Compression

    $ tar -zcvf 2022.tar.gz 2022

    Compression with time information

    $ tar -zcf etc_$(date +%F).tar.gz /etc$ tar -jcf root_$(date +%Y.%m.%d-%H%M%S).tar.bz2 /root$ tar -jcf root_$(date +%y.%m.%d-%H%M%S).tar.bz2 /root


    4. Extract

    $ tar -zxvf 2022.tar.gz$ tar -zxvf 2022.tgz$ tar -jxvf 2022.tar.bz2$ tar -Jxvf 2022.tar.Z$ tar -xf 2022.tar.gz


    By default, it is restored to the original package path. You can use the option "-C" to specify the decompressed directory.

    $ tar -zxf /opt/etc.tar.gz$ tar -zxf /opt/etc.tar.gz -C /root


    4. 3. view the file structure of the compressed package

    View the compressed file list

    $ tar -tf etc.tar.gz

    View the compressed file list in detail

    $ tar -tvf etc.tar.gz



    4. skip some files during packaging

    Options "-X" and "-- exclude"

    $ tar --exclude=/data/tomcat_cb/logs/* -zcvf  tom.tgz /data/tomcat_cb/


    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.