Using the tar command under Linux

Source: Internet
Author: User
Tags gz file

This article was transferred from: http://www.cnblogs.com/li-hao/archive/2011/10/03/2198480.html

Extract
Syntax: tar [main option + Secondary options] file or directory

When using this command, the main option is required, and it tells Tar what to do, auxiliary options are auxiliary and can be used.

Main options:

C Create a new profile. Select this option if the user wants to back up a directory or some files. Equivalent to packaging.

X release the file from the archive file. Equivalent to unpacking.

T list the contents of the archive file and see which files have been backed up.

In particular, in the release of the parameters, c/x/t can only exist one! Cannot exist at the same time! Because it is not possible to compress and decompress simultaneously.

Accessibility Options:

-Z: Do you have the properties of gzip at the same time? i.e. do I need to compress or decompress with gzip? The general format is xx.tar.gz or XX. tgz

-j: Do you have bzip2 properties at the same time? i.e. do I need to compress or decompress with bzip2? The general format is xx.tar.bz2

-V: Files are displayed during compression! This common

-F: Use the file name, please note, after F to immediately answer the file name Oh! Don't add any more parameters!

-P: Use original file properties (attributes are not changed according to user)

--exclude file: In the process of compression, do not package file!

Example:

Example one: Package all the files in the/etc directory into/tmp/etc.tar

[[email protected] ~]# TAR-CVF/TMP/ETC.TAR/ETC <== package only, do not compress!

[[email protected] ~]# tar-zcvf/tmp/etc.tar.gz/etc <== packaged, compressed with gzip

[[email protected] ~]# tar-jcvf/tmp/etc.tar.bz2/etc <== packaged, bzip2 compressed

# Note that the file name after parameter f is taken by yourself, and we are accustomed to using. Tar as a recognition.

# if the z parameter is added, the. tar.gz or. tgz represent the gzip compressed tar file ~

# If you add the J parameter, use. tar.bz2 as the file name.

# When the above instruction is executed, a warning message is displayed:

# "tar:removing leading"/"from member names" that is a special setting for absolute paths.

Example two: Check out what files are in the above/tmp/etc.tar.gz file?

[Email protected] ~]# tar-ztvf/tmp/etc.tar.gz

# because we use gzip compression, so to check the file in the tar file,

# you have to add Z to this parameter! It's important!

Example three: Extracting the/tmp/etc.tar.gz file under/USR/LOCAL/SRC

[Email protected] ~]# CD/USR/LOCAL/SRC

[Email protected] src]# tar-zxvf/tmp/etc.tar.gz

# in the case of presets, we can unzip the file anywhere! In this example,

# I'm going to transform my working directory under/USR/LOCAL/SRC and untie/tmp/etc.tar.gz

# then the unpacked directory will be in/usr/local/src/etc, plus, if you enter/USR/LOCAL/SRC/ETC

# you will find that the file attributes in this directory may be different from the/etc/.

Example four: under/tmp, I just want to untie the etc/passwd inside the/tmp/etc.tar.gz.

[Email protected] ~]# cd/tmp

[Email protected] tmp]# tar-zxvf/tmp/etc.tar.gz etc/passwd

# I can check the file name in the Tarfile through TAR-ZTVF, if only one file,

# can be released in this way! Notice that! The root directory within the etc.tar.gz/is taken away!

Example five: I want to back up home/home, etc, but don't/home/dmtsai

[Email protected] ~]# tar--EXCLUDE/HOME/DMTSAI-ZCVF myfile.tar.gz/home/*/etc

In addition: the C parameter of the TAR command

$ TAR-CVF File2.tar/home/usr2/file2
Tar:removing leading '/' from the Members names
Home/usr2/file2
This command can package the/home/usr2/file2 file into the File2.tar in the current directory, note that the source file identified with the absolute path, after compression with the tar command, with the absolute path (here is the home/usr2/, root directory '/' is automatically removed) is compressed in the same. The following conditions occur when decompressed with the tar command:
$ TAR-XVF File2.tar
$ ls
.............. .....
The extracted file name is not the imagined file2, but the home/usr2/file2.

$ TAR-CVF file2.tar-c/HOME/USR2 file2
The-C dir parameter in this command changes the working directory of tar from the current directory to/HOME/USR2 and compresses the File2 file (without an absolute path) into File2.tar. Note: The function of the-C dir parameter is to change the working directory, which is valid until the next time the command-C dir parameter.
Using the-C dir parameter of tar, it is also possible to extract files to other directories under the current directory/HOME/USR1, for example:
$ TAR-XVF file2.tar-c/HOME/USR2
The tar cannot be done without the-C dir parameter:
$ TAR-XVF FILE2.TAR/HOME/USR2
Tar:/tmp/file:not found in archive
Tar:error exit delayed from previous errors

Using the tar command 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.