Detailed usage of tar, Gzip, and unzip commands

Source: Internet
Author: User
Tags gz file

Tar command

Tar can create files and directories. Using tar, you can create a file (backup file) for a specific file, change the file in the file, or add a new file to the file. Tar was originally used to create files on tape. Now, you can create files, such as floppy disks, on any device. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission. Tar on Linux is of the GNU version.

Syntax: Tar [primary option + secondary option] file or directory

When you use this command, the main option is required, and it tells tar what to do. The auxiliary option is used for assistance and can be used.

Main options:

C. Create a new archive file. Select this option if you want to back up a directory or some files.

R: append the file to the end of the file. For example, if you have prepared a backup file and find that there is still a directory or some files have forgotten to be backed up, you can use this option to append the directory or files you have forgotten to the backup file.

T list the file content and check which files have been backed up.

U updates the file. That is to say, replace the original backup file with the new file. If the file to be updated cannot be found in the backup file, append it to the end of the backup file.

X release a file from the file.

Auxiliary options:

B. This option is set for the tape drive. It is followed by a number to describe the block size. The default value is 20 (20*512 bytes ).

F. This option is usually required when you use an archive file or device.

K. Save the existing files. For example, if we restore a file, the same file will not be overwritten during restoration.

M sets the modification time of all files to the present when restoring files.

M creates a multi-volume archive file to store it on several disks.

V detailed report on the file information processed by tar. If this option is not available, tar does not report file information.

W each step requires confirmation.

Z uses gzip to compress/decompress the file. With this option, the file can be compressed. However, you must use this option to decompress the file during restoration.

Example 1: create a full-part sub-directory file named usr.tar in the/home directory.

$ Tar CVF usr.tar/home

Example 2: complete the sub-directories in the/home directory and compress them with the name usr.tar.gz.

$ Tar czvf usr.tar.gz/home

Example 3: Restore and decompress the backup file usr.tar.gz.

$ Tar xzvf usr.tar.gz

Example 4: The contents of the usr.tar backup file are displayed on the display in split screen mode.

$ Tar tvf usr.tar | more

To back up a file to a specific device, you only need to use the device name as the backup file name.

Example 5: You can create a backup file in a floppy disk of the/dev/fd0 device and copy all the files in the/home directory to the backup file.

$ Tar CF/dev/fd0/home

To restore files on a device disk, use the xf option:

$ Tar xf/dev/fd0

If the size of the backup file exceeds the storage space available for the device, such as a floppy disk, you can create a multi-volume tar backup file. The m option indicates that the tar command prompts you to use a new storage device. When you use the M option to archive data to a soft drive, when a floppy disk is full, the tar command reminds you to add a new floppy disk. In this way, you can save the tar file to several disks.

$ Tar CMF/dev/fd0/home

To restore the files on several disks, you only need to put the first disk into the soft drive, and then enter the tar command with the X and m options. If necessary, you will be reminded to add another floppy disk.

$ Tar xmf/dev/fd0

Gzip command

There are two obvious advantages to reduce the file size. One is to reduce the storage space, and the other is to reduce the transmission time when the file is transmitted over the network. Gzip is a frequently used command in Linux to compress and decompress files, which is convenient and easy to use.

Syntax: gzip [Option] compressed (decompressed) file name

Meaning of each option:

-C writes the output to the standard output and keeps the original file.

-D. decompress the compressed file.

-L the following fields are displayed for each compressed file:

Size of the compressed file

Size of uncompressed files

Compression Ratio

Uncompressed file name

-R recursively searches for the specified directory and compresses all the files or decompress the files.

-T test to check whether the compressed file is complete.

-V displays the file name and compression ratio for each compressed and decompressed file.

-Num uses the specified numeric num to adjust the compression speed.-1 or -- fast indicates the fastest compression method (low compression ratio), and-9 or -- best indicates the slowest compression method (high compression ratio ). The default value is 6.

Assume that a directory/homehas the mm.txtw.sort.txt and files.

Example 1: compress each file in the/homecatalog into A. GZ file.

$ CD/home

$ Gzip *

$ Ls

M.txt.gz sort.txt.gz

Example 2: extract each compressed file in Example 1 and list the detailed information.

$ Gzip-DV *

Mm.txt.gz 43.1% ----- replaced with mm.txt

Sort.txt.gz 43.1% ----- replaced with sort.txt 43.1% ----- replaced with

$ Ls

Mm.txt sort.txt

Example 3: detailed information of each compressed file in Example 1 is displayed without decompression.

$ Gzip-L *

Compressed uncompr. ratio uncompressed_name

277 445 43.1% mm.txt

278 445 43.1% sort.txt

277 445 43.1%

$ Ls

Mm.txt.gz sort.txt.gz

Example 4: compress a Tartar file, for example, usr.tar. the extension name of the compressed file is .tar.gz.

$ Gzip usr.tar

$ Ls


Unzip command

How can I expand files compressed by WinZip in Microsoft Windows in Linux? You can use the unzipcommand to unexpand a compressed file named. Zip.

Syntax: unzip [Option] compressed file name. Zip

The meanings of each option are as follows:

-X file list: Decompress the file, but does not include the specified file.


-T whether the test file is damaged, but the pressure is not solved.

-D directory: Decompress the compressed file to the specified directory.

-Z only displays the annotation of the compressed file.

-N does not overwrite existing files.

-O overwrites existing files and does not require user confirmation.

-J. Do not recreate the directory structure of the document and decompress all the files to the same directory.

Example 1: extract from the current directory.

$ Unzip

Example 2: Decompress the compressed file under the specified directory/tmp. If the same file already exists, the unzip command must not overwrite the original file.

$ Unzip-N

Example 3: view the directory of the compressed file.

$ Unzip-V

Zgrep command

The function of this command is to find a matching regular expression in a compressed file. The usage is the same as that of the grep command, except that the object operated is a compressed file. You can use the zgrep command to check whether a certain sentence exists in a compressed file.

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.