Package and compress files or directories under Linux

Source: Internet
Author: User
Tags unpack

One, compression command [COMPRESS/GZIP/BZIP2]:

Compression can only be done on files, not directories, directories to be packaged first, and then compressed.

(1) Compress

(2) Gzip

1. Compress files

[CPP]View PlainCopy
    1. Gzip hosts #将hosts压缩成hosts. GZ, do not retain the original file
    2. Gzip-c hosts > Xx/hosts.gz #将hosts压缩成hosts. GZ, preserves the original file, and can also specify a path

2. View Compressed file contents

[CPP]View PlainCopy
    1. Zcat hosts.gz

3. Decompression

[CPP]View PlainCopy
    1. Gzip-d hosts.gz #不指定路径的解压, unzip to the current directory, do not retain the compressed file
    2. Gzip-d-C hosts.gz > Xx/hosts #指定路径的解压 while preserving compressed files

(3) bZIP

1. Compress files

[CPP]View PlainCopy
    1. Bzip2 hosts.bz2 #将hosts压缩成hosts. bz2, do not keep the original file
    2. Bzip2-c hosts > xx/hosts.bz2 #将hosts压缩成hosts. GZ, retains the original file, and can also specify the path <span style="FONT-SIZE:18PX;" >
    3. </span>

2. View Compressed file contents

[CPP]View PlainCopy
    1. Bzcat hosts.bz2

3. Decompression

[CPP]View PlainCopy
    1. Bzip2-d hosts.bz2 #不指定路径的解压, unzip to the current directory, do not retain the compressed file
    2. Bzip2-d-C hosts.bz2 > Xx/hosts #指定路径的解压 while preserving compressed files

Summarize:

-C: Specify path, keep file

-D: Unzip

gzip, bzip2 default does not retain the original file/compressed file.

Ii. Packing/compressing commands [tar]

2.1 Packing/Unpack:

(1) Package a single file

[CPP]View PlainCopy
    1. Tar cvf aa.tar AA #默认保留原文件
    2. Tar cvf aa.tar aa--remove-file #打包后删除原文件

(2) Packaging multiple files

[CPP]View PlainCopy
    1. Tar cvf ab.tar aa bb #默认保留原文件
    2. Tar cvf aa.tar aa bb--remove-file #打包后删除原文件

(3) View the contents of the package

[CPP]View PlainCopy
    1. Tar TVF Aa.tar

(4) Unpacking

[CPP]View PlainCopy
    1. Tar xvf Aa.tar #不指定路径的解包, unpack to current directory
    2. Tar xvf aa.tar-c xx/aa #指定路径的解包

2.2 Package and compress/unzip and unpack:

In gzip format

(1) Package a single file and compress

[CPP]View PlainCopy
    1. Tar zcvf aa.tar.gz AA #保留原文件
    2. Tar zcvf aa.tar.gz aa--remove-file #不保留原文件

(2) Packaging multiple files and compressing

[CPP]View PlainCopy
    1. Tar zcvf ab.tar.gz aa bb #保留原文件
    2. Tar zcvf ab.tar.gz aa bb--remove-file #不保留原文件

(3) Decompression

[CPP]View PlainCopy
    1. Tar zxvf ab.tar.gz #不指定路径解压, unpacking
    2. Tar zxvf ab.tar.gz-c xx/ab #指定路径解压, unpacking

In BIZP2 format

(1) Package a single file and compress

[CPP]View PlainCopy
    1. Tar jcvf aa.tar.bz2 AA #保留原文件
    2. Tar jcvf aa.tar.bz2 aa--remove-file #不保留原文件

(2) Packaging multiple files and compressing

[CPP]View PlainCopy
    1. Tar jcvf ab.tar.bz2 aa bb #保留原文件
    2. Tar jcvf ab.tar.bz2 aa bb--remove-file #不保留原文件

(3) Decompression

[CPP]View PlainCopy
    1. Tar jxvf ab.tar.bz2 #不指定路径解压, unpacking
    2. Tar jxvf ab.tar.bz2-c xx/ab #指定路径解压, unpacking


Summarize:

-C: Specify the path

--remove-file: Delete the original file

The tar command retains the original file by default.

Package and compress files or directories under Linux

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: info-contact@alibabacloud.com 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.