Linux package and compression command-Tar

Source: Internet
Author: User

Package A pile of files into a file. It is better to mark it with a suffix. Tar. Because only one file can be compressed in Linux, We need to package the file before compression. Fortunately, the single tar command is responsible for packaging, unpackaging, compression, and unzipping. All different operations are given through parameters.

 Package/unpackage Parameters:

-C: Create a parameter command for the compressed file );
-X: Unlock the parameter command of a compressed file!
-T: view the files in the tarfile!

Note that C/X/T can only exist under the parameter! It cannot exist at the same time, because it is impossible to compress and decompress simultaneously.

 Compression/Decompression Parameters:

-Z: does it have the gzip attribute at the same time? That is, do I need to use gzip for compression?

-J: Does it have Bzip2 attributes at the same time? That is, do I need to use Bzip2 for compression?

 Other common parameters:

-V: The file is displayed during compression! This is common, but it is not recommended to use it in the background execution process!
-F: use the file name. Please note that the file name should be followed immediately after F! Do not add parameters! (It can be understood as the last parameter)
-P: use the original attributes of the original file (the attributes will not be changed based on the user)
-P: absolute paths can be used for compression!
-N: a new date (yyyy/mm/DD) will be packed into the new file!
-- Exclude file: Do not package the file during compression.

Example:

Example 1: package all the files in the/etc directory into/tmp/etc.tar
[Root @ Linux ~] # Tar-CVF/tmp/etc.tar/etc
<= Package only, not compressed!
[Root @ Linux ~] # Tar-zcvf/tmp/etc.tar.gz/etc <= compressed with Gzip
[Root @ Linux ~] # Tar-jcvf/tmp/etc.tar.bz2/etc <= compressed with Bzip2

# Note that the file name after parameter F is obtained by ourselves. We use. tar for identification.
# If the Z parameter is added, .tar.gz or. tgz is used to represent the compressed tar file in gzip.
# If you add the J parameter, use .tar.bz2 as the file name.
# When the preceding command is executed, a warning message is displayed: [tar: removing leading '/"from Member names], which is a special setting for absolute paths.

Example 2: Check the files in the above/tmp/etc.tar.gz file?

[Root @ Linux ~] # Tar-ztvf/tmp/etc.tar.gz

# Because Gzip is used for compression, you must add the Z parameter when you want to view the files in the TAR file! This is important!

Example 3: Decompress the/tmp/etc.tar.gz file under/usr/local/src.

[Root @ Linux ~] # Cd/usr/local/src
[Root @ Linux SRC] # tar-zxvf/tmp/etc.tar.gz

# By default, We Can uncompress files anywhere! In this example, I first transform the working directory to the/usr/local/src directory, and unbind/tmp/etc.tar.gz, the directory to be unlocked will be in/usr/local/src/etc! In addition, if you enter/usr/local/src/etc, you will find that the file attributes in this directory may be different from those in/etc! Consider the-p parameter.

Example 4: Under/tmp, I only want to unbind the etc/passwd in/tmp/etc.tar.gz.

[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # tar-zxvf/tmp/etc.tar.gz etc/passwd

# I can use tar-ztvf to check the file name in the tarfile. If you only need one file, you can issue it in this way! Notice! The root directory in etc.tar.gz/is removed!

Example 5: Back up all the files in/etc/and save their permissions!

[Root @ Linux ~] # Tar-zxvpf/tmp/etc.tar.gz/etc

# This-P attribute is very important, especially when you want to keep the attributes of the original file!

Example 6: only new files in/home are backed up.

[Root @ Linux ~] # Tar-n "2005/06/01"-zcvf home.tar.gz/home

Example 7: I want to package files under/home and/etc, but I need to exclude CVS |. SVN folder and some SWP files.

[Root @ Linux ~] # Tar-jcvf myfile.tar.bz2/home/etc -- exclude = "CVS" -- exclued = ". SVN" -- exclude = "*. SWP"

 Example 8: package/etc/and unpack it under/tmp without generating a file! (This is not usually used. Ignore it)

[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # tar-CVF-/etc | tar-xvf-

# This action is a bit like CP-r/etc/tmp ~ It is still useful!
# Note that the output file is changed to-and the input file is changed to-, and there is another file | Yes ~
# This represents standard output, standard input, and pipeline commands respectively!

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.