Linux feature file archiving and compression (tar, file, Zip)

Source: Internet
Author: User
Tags bz2 rar uncompress

This paper mainly from the following several convenient to explain file archiving and compression, while comparing the compression ratios and characteristics of several different compression methods.

    • File Archive command tar,tar.gz source package installation Management
    • Create a Tar Package-extract-Query the contents of the TAR Package
    • Zip Use of Commands

why compress?

1. easy to use, query, read

2. Easy to manage (delete files in bulk)

: Host A to transmit a file size of 10G with Host B is estimated to transmit 100s.

If the direct transfer takes a lot of traffic bandwidth, the company's intranet access is slow.

compressed before transfer -- Decompression after transfer

I compress the 10G file into 5G and the transmission time is 50s.

Benefits of File Compression:

    1. Conserve hard disk resources.
    2. Speed up the file transfer.
1. File Archive Command1.1. Use of the tar command

Tar file. Is the collection of several files and/or directories in a folder. is the best tool for creating backup and archive

Role: packaging, compressing files

[Email protected] ~]# tar--help

Usage:tar [OPTION ...] [FILE] ...

GNU ' tar ' saves many files together into a single tape or disk archive, and can

Restore individual files from the archive.

Examples:

TAR-CF Archive.tar Foo Bar # Create Archive.tar from Files foo and bar. Create an archive

TAR-TVF Archive.tar # List all files in Archive.tar verbosely. Listing all Documents

tar-xf Archive.tar # Extract all file from Archive.tar. Extract all files from Archive.tar .

Tar option package Name destination file/directory[email protected] ~]# tar cvf aa.tar a.out who.out touch.txt #归档

# C Create Create

#v Detail

#f filename

[Email protected] ~]# tar TVF aa.tar #查看包里的文件

[Email protected] ~]# tar XF aa.tar #解包

1.2 File command

Linux The extension of the file is not as strict as Windows requirements, so in the process of using Linux often encountered some files do not have the extension, where should we judge the file without the extension, in the end is a file or directory it?

Role: Determine file type

Syntax: File file name

[email protected] ~]# Touch a.txt
[[email protected] ~]# file A.txt
A.txt:empty
[email protected] ~]# file/etc/passwd
/etc/passwd:ascii Text
[email protected] ~]# file/home/berners
/home/berners:directory
[email protected] ~]# file/etc/init.d/network
/etc/init.d/network:bourne-again shell script, ASCII text executable
[email protected] ~]#

Example: Package two directories or target + files into one package

[Email protected] ~]# tar cvf bb.tar/boot//etc/passwd

1.3 Don't understand the package and see what's in tar:

[Email protected]xiaolyu ~]# tar tvf bb.tar

Operation-Unpacking:

[email protected] ~]# tar xvf Bb.tar # Unzip the Bb.tar

Note: If the unpacking path is not specified during the unpacking process, the original path will be unpacked and overwritten, especially if the files under the original path have been modified after packaging.

Action-Unzip the specified path:

- C # Specify the location of the extracted path

[email protected] ~]# tar xvf aa.tar-c/home/berners/# unzip the Bb.tar into the/home/berners directory

[[email protected] ~]# ls
Aa.tar Documents kernel.txt Pictures Templates
Anaconda-ks.cfg Downloads Music Public Videos
Desktop initial-setup-ks.cfg passwd.txt redhat.txt who.out
[Email protected] ~]# tar XF aa.tar-c/home/berners
[Email protected] ~]# ls/home/berners
A.out Touch.txt Who.out
[Email protected] ~]#

Action-Compare size of File:

[Email protected] ~]# Du-sh Aa.tar
20kaa.tar
[Email protected] ~]# ll-h/home/berners/aa.tar
-rw-r--r--1 root root 20K August 11:22/home/berners/aa.tar
[Email protected] ~]#

2. Tar Archive + compression

Gzip bzip2 Zip tar

One, compression format: GZ, bz2, XZ, zip, Z

Format (file name format):. tar.gz or. tgz

Syntax format: Tar zcvf newfile.tar.gz SOURCE

[email protected] ~]# tar zcf aa.tar.gz/home/berners/# compression

2.1 Compare the size of the file before and after the tar zcf compression.

[Email protected] ~]# ll-h aa.tar*

-rw-r--r--1 root root 20K August 10:37 Aa.tar//20k before compression.
-rw-r--r--1 root root 3.7K August 12:51 aa.tar.gz//Compressed 3.7k.

Compress the file with tar zcf before compression (20k)/after compression (3.7k) = 5.4 times times.

Maybe you want to say my file is relatively small, then I'll get a big file compare:

[Email protected] ~]# Du-sh share.tar*
1.2gshare.tar
457mshare.tar.gz
[Email protected] ~]#

This file is larger, pre-compression and post-compression ratios of 1.2*1024/457 = 2.69 times times.

Conclusion: The compression ratio of tar zcf is still very low. And the larger the file, the lower the shrinking ratio.

2.2 Decompression

Syntax format: Tar zxvf xx.tar.gz.

V can be omitted, V is used to indicate whether the operation process is displayed.

[email protected] ~]# tar zxf aa.tar.gz-c/home/lisi
[email protected] ~]# ls-l/home/berners
Total
-rw-r--r--1 root root 20480 August 11:22 Aa.tar
-rwxr-xr-x 1 root root 8547 August 19:46 a.out
-rw-r--r--1 dabai xiaobai 0 August 07:27 touch.txt
-rw-r--r--1 root root 143 August 12:23 who.out
[email protected] ~]# ls-l/home/lisi
Total 0
drwxr-xr-x 3 root root 20 August 13:31 home
[[Email protected] ~]# CD home
[[email protected] home]# ls
Berners
[email protected] home]# CD Berners
[email protected] berners]# ls-l
Total
-rw-r--r--1 root root 20480 August 11:22 Aa.tar
-rwxr-xr-x 1 root root 8547 August 19:46 a.out
-rw-r--r--1 dabai xiaobai 0 August 07:27 touch.txt
-rw-r--r--1 root root 143 August 12:23 who.out

3. Archive + Compress: bz2

Format (file name format):. tar.bz2

Syntax format: Tar jcvf newfile.tar.bz2 SOURCE

4. zip Package Decompression Command 4.1 zip is the compression program, unzip is the decompression program.

Compress files

[Email protected] ~]# zip passwd.zip/etc/passwd

- R Compressed Directory

Format "Zip option name source"

[Email protected] ~]# zip-r grub2.zip/boot/grub2/

[Email protected] ~]# ll-h grub2.*

-rw-r--r--1 root root 7.7M Feb 07:40 Grub2.tar

-rw-r--r--1 root root 2.5M Feb 08:02 grub2.tar.bz2

-rw-r--r--1 root root 3.1M Feb 07:56 grub2.tar.gz

-rw-r--r--1 root root 3.2M Feb 08:11 grub2.zip

4.2 Decompression

[Email protected] ~]# Unzip grub2.zip-d/opt/

- D Specify the path

---------------------------------------------The following information: http://blog.hehehehehe.cn/a/17148.htm

How to decompress under Linux. tar.bz2 format for compressed packages older versions of Linux take two steps, one step is decompression, one step is unpacking
Bzip2-d **.tar.bz2//Unzip the file into **.tar
TAR-XF **.tar//Unpacking
Now the new Linux version, as long as a single step can be extracted
TAR-XF **.tar.bz2

Summary of tar.gz, tar, bz2, zip and other compression commands under Linux


The most common packaging program under Linux is tar, which is often referred to as the TAR package, and the tar package file commands are usually terminated with. Tar. After the tar package is generated, you can use other programs to enter the
Row is compressed, so let's start with the basic usage of the TAR command:
There are many options for the tar command (which can be viewed with man tar), but there are a few options that are commonly used, the following
To illustrate:
# TAR-CF All.tar *.jpg
This command is to make all. jpg files into a package named All.tar. -C is a means of generating a new package
,-f Specifies the file name of the package.
# TAR-RF All.tar *.gif
This command adds all the. gif files to the All.tar package. -R is a representation of the added file
Mean.
# Tar-uf All.tar logo.gif
This command is to update the original tar package All.tar in the logo.gif file,-U is to indicate the meaning of the update file.

# TAR-TF All.tar
This command is to list all the files in the All.tar package,-T is to list the meaning of the file
# TAR-XF All.tar
This command is to solve all the files in the All.tar package,-X is the meaning
These are the most basic uses of tar. In order to facilitate the user in the package unpacking can be compressed or decompressed
File, Tar provides a special feature. This is the tar that can be packaged or unpacked while calling other pressure
Programs such as Gzip, bzip2, and so on.
1) Tar call gzip
Gzip is a compression program developed by the GNU organization, and the. Gz end file is the result of gzip compression. With gzip
The relative decompression procedure is gunzip. Use the-Z parameter in tar to invoke gzip. Here's an example to illustrate

# TAR-CZF all.tar.gz *.jpg
This command is to make all. jpg files into a tar package and compress them with gzip to generate a
Gzip compressed package, package named all.tar.gz
# TAR-XZF All.tar.gz
This command unlocks the package generated above.
2) Tar call bzip2
Bzip2 is a more compressible compression program, and the. bz2 end of the file is the result of bzip2 compression.
The decompression procedure relative to bzip2 is bunzip2. Use the-j parameter in tar to invoke gzip. Here's an example.
Explain:
# TAR-CJF all.tar.bz2 *.jpg
This command is to make all. jpg files into a tar package and compress them with bzip2 to generate a
BZIP2 compressed package, package named all.tar.bz2
# TAR-XJF ALL.TAR.BZ2
This command unlocks the package generated above.
3) Tar call compress
Compress is also a compression program, but it seems that people who use compress are not as good as gzip and bzip2.
Many.. The file at the end of Z is the result of bzip2 compression. The decompression procedure relative to compress is uncompress
。 Use the-Z parameter in tar to invoke compress. Here's an example:
# TAR-CZF All.tar.z *.jpg
This command is to make all. jpg files into a tar package and compress them with compress to generate
A uncompress compressed package with the package named All.tar.z
# TAR-XZF All.tar.z
This command unlocks the package that was created above.

With the above knowledge, you should be able to solve a variety of compressed files, the following for the TAR series of compressed text
A summary of the pieces:
1) for files ending in. tar
TAR-XF All.tar
2) for files ending in. gz
Gzip-d all.gz
Gunzip all.gz
3) for files ending with. tgz or. tar.gz
Tar-xzf all.tar.gz
Tar-xzf all.tgz
4) files ending with. bz2
Bzip2-d all.bz2
BUNZIP2 all.bz2
5) For tar.bz2 end of file
TAR-XJF all.tar.bz2
6) for. Z End of File
Uncompress all. Z
7) files ending with. tar.z
Tar-xzf All.tar.z

In addition to the common compressed files under window. zip and. Rar,linux also have a corresponding way to decompress it
:
1) for. zip
Linux provides zip and unzip programs, ZIP is a compression program, unzip is the decompression program. They have a number of parameter
options, here is a brief introduction, and still illustrate its use:
# zip all.zip *.jpg
This command is to compress all. jpg files into a zip package
# unzip All.zip
This command extracts all the files in the All.zip
2) for. RAR
to process. rar files under Linux, you need to install RAR for Linux, which can be downloaded from the Web, but remember that RAR for Linux is not free; available from http:/ /www.rarsoft.com/download.htm Download Rarfor Linux 3.2.
0, then install:
# TAR-XZPVF rarlinux-3.2.0.tar.gz
# cd RAR
# Make
This is installed, after the installation of RAR and unrar these two programs, RAR is a compression program, Unrar is the decompression program. They have a lot of parameter options, here are just a brief introduction, still illustrate its usage:

# rar a All *.jpg
This command compresses all. jpg files into a RAR package named All.rar, which will be. rar
The extension is automatically appended to the package name.
# Unrar E All.rar
This command is to extract all the files in the All.rar.
To this, we have introduced the Linux tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, RAR, Unrar and other programs, you should already be able to use them to. tar,. GZ,. tar.gz,. tgz,. bz2,. tar.bz2,. Z,. Tar. Z,. zip,. rar 10 Kinds of compressed text
, you should not need to download a software and do not know how to solve Linux under the trouble. And the above methods are basically effective for UNIX.
This article describes the programs that are compressed under Linux, such as tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, RAR, Unrar, and how to use them for. Tar,. GZ,. tar.gz,. tgz,. bz2,. tar.bz2,. Z. Tar. 10 compressed Files of Z,. zip,. rar
Operation.

The following additions

Tar

-C: Create compressed archives
-X: Unzip
-T: View content
-r: Append files to the end of a compressed archive file
-U: Update files in original compressed package

These five are stand-alone commands, which use one of the compression decompression, and can be used with other commands but only one. The following parameters are optional when compressing or extracting files as needed.

-Z: With gzip properties
-j: With the bz2 attribute
-Z: With the Compress attribute
-V: Show All procedures
-O: Unpack the file to standard output

The following parameter-F is required
-F: Use the file name, remember that this parameter is the last parameter, can only be followed by the file name.
# TAR-CF All.tar *.jpg This command is to make all. jpg files into a package called All.tar. -C is the file name that represents the generation of a new package,-f specifies the package.
# TAR-RF All.tar *.gif
This command adds all the. gif files to the All.tar package. -R is meant to increase the file.
# Tar-uf All.tar logo.gif
This command updates the logo.gif file in the original tar package All.tar, and-u means the update file.
# TAR-TF All.tar
This command is to list all the files in the All.tar package,-T is the meaning of the list file
# tar-xf All.tar
This command is to solve all the files in the All.tar package,-X is to untie the meaning of
compression
TAR–CVF Jpg.tar *.jpg//package all JPG files in the directory into tar.jpg
Tar–czf jpg.tar.gz *.jpg// After packaging all the JPG files in the directory into Jpg.tar and compressing them in gzip, generate a gzip compressed package named jpg.tar.gz
TAR–CJF jpg.tar.bz2 *.jpg// After packaging all the JPG files in the directory into Jpg.tar and compressing them with bzip2, generate a bzip2 compressed package named jpg.tar.bz2
Tar–czf jpg.tar.z *.jpg// After packaging all the JPG files in the directory into Jpg.tar, and compressing them with compress, generate a umcompress compressed package named Jpg.tar.z
rar a jpg.rar *.jpg//rar format compression, you need to download the RAR first For Linux
Zip jpg.zip *.jpg//zip format compression, you need to download zip for Linux first

Extract
TAR–XVF File.tar//Unpacking the TAR Package
TAR-XZVF file.tar.gz//Decompression tar.gz
TAR-XJVF file.tar.bz2//Decompression tar.bz2
TAR–XZVF file.tar.z//Unzip tar. Z
Unrar e file.rar//decompression rar
Unzip File.zip//Unzip zip

Summarize
1, *.tar with TAR–XVF decompression
2, *.gz with gzip-d or gunzip decompression
3, *.tar.gz and *.tgz with TAR–XZF decompression
4, *.bz2 with bzip2-d or with BUNZIP2 decompression
5, *.tar.bz2 with TAR–XJF decompression
6, *. Z Extract with Uncompress
7, *.tar. Z Extract with Tar–xzf
8, *.rar with Unrar e decompression
9, *.zip with unzip decompression

Reference: http://blog.hehehehehe.cn/a/17148.htm

Linux feature file archiving and compression (tar, file, Zip)

Related Article

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.