One Linux command per day (file upload download file operation): "Reprint" tar Command

Source: Internet
Author: User
Tags ssh access

Through SSH access to the server, will inevitably use compression, decompression, packaging, unpacking, etc., this time the tar command is an essential tool for a powerful. The most popular tar in Linux is the perfectly formed, the most powerful.

The TAR command creates archives for Linux files and directories. With tar, you can create files (backup files) for a specific file, or you can change files in the file or add new files to the file. Tar was originally used to create archives on tape, and now users can create files on any device. With the tar command, you can package a whole bunch of files and directories into a single file, which is useful for backing up files or combining several files into one file for easy network transmission.

The first thing to understand is two concepts: packaging and compression. Packaging refers to a large number of files or directories into a total file, compression is a large file through some compression algorithm into a small file.

Why should we differentiate between these two concepts? This is due to the many compression programs in Linux that can only be compressed for one file, so that when you want to compress a lot of files, you have to first make a package (Tar command) of the whole bunch of files, and then compress the program (gzip bzip2 command).

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 compress it.

1. Command format:

tar[necessary parameters [selection parameters] [file]

2. Command function:

Used to compress and decompress files. The tar itself does not have compression capabilities. It is implemented by invoking the compression function

3. Command parameters:

The necessary parameters are as follows:

-A add compressed file to existing compression

-B Set Chunk size

-C Create a new compressed file

-D Record File differences

-R Add a file to a file that has been compressed

-u add changed and existing files to a compressed file that already exists

-X extracting files from compressed files

-T displays the contents of the compressed file

-Z Support gzip unzip files

-j Support bzip2 Extract files

-Z Support Compress unzip files

-V Show Operation procedure

-L File System boundary settings

-K Keep the original file without overwriting

-m reserved files are not overwritten

-W confirm the correctness of the compressed file

The optional parameters are as follows:

-B Set Number of blocks

-c switch to the specified directory

-f Specifies the compressed file

--HELP Display Help information

--version displaying version information

4. Common decompression/Compression commands

Tar
Unpacking: Tar xvf Filename.tar
Package: Tar cvf filename.tar DirName
(Note: Tar is packaged, not compressed!) )


. gz
Decompression 1:gunzip filename.gz
Decompression 2:gzip-d filename.gz
Compression: gzip FileName

.tar.gz  and  .tgz
decompression: tar zxvf filename.tar.gz
compression: Tar zcvf filename.tar.gz dirname

.bz2
decompression 1:bzip2 -d filename.bz2
Unzip 2:bunzip2 filename.bz2
compression:  bzip2 -z  filename

.tar.bz2
decompression: tar jxvf filename.tar.bz2
compression: Tar jcvf filename.tar.bz2 dirname

.bz
decompression 1:bzip2 -d filename.bz
Unzip 2:bunzip2 filename.bz
compression: unknown

. tar.bz
Decompression: Tar jxvf FileName.tar.bz
Compression: Unknown

. Z
Decompression: uncompress filename.z
Compression: Compress FileName

. Tar. Z
Decompression: Tar zxvf filename.tar.z
Compression: Tar zcvf filename.tar.z DirName

. zip
Decompression: Unzip Filename.zip
Compression: Zip Filename.zip DirName

. rar
Decompression: rar x Filename.rar
Compression: rar a Filename.rar DirName

5. Working with instances

Example 1: Package all files into a tar package

Command:

TAR-CVF Log.tar Log2012.log

TAR-ZCVF log.tar.gz Log2012.log

TAR-JCVF log.tar.bz2 Log2012.log

Output:

[email protected] test]# LS-Al Log2012.log---xrw-r--1Root root302108  One- -  .:Genevalog2012.log[[email protected] test]# tar-CVF Log.tar log2012.log log2012.log[[email protected] test]# tar-ZCVF log.tar.gz log2012.loglog2012.log[[email protected] test]# tar-JCVF log.tar.bz2 log2012.loglog2012.log[[email protected] test]# ls-al *.tar*-rw-r--r--1Root root307200  One- in  -: WuLog.tar-rw-r--r--1Root root1413  One- in  -: -log.tar.bz2-rw-r--r--1Root root1413  One- in  -: WuLog.tar.gz

Description

TAR-CVF Log.tar Log2012.log Package only, do not compress!

TAR-ZCVF log.tar.gz log2012.log After packing, compress with gzip

TAR-ZCVF log.tar.bz2 Log2012.log is packaged to bzip2 compression

The file name after the parameter F is taken by itself, and we are accustomed to using. Tar as an identification. If the z parameter is added, a. tar.gz or. tgz is used to represent the gzip compressed tar package, or the. tar.bz2 as the TAR package name if the J parameter is added.

Example 2: Check which files are in the tar package above

Command:

TAR-ZTVF log.tar.gz

Output:

[Email protected] test]# tar-ZTVF log.tar.gz---xrw-r--root/root    302108-11 -: From:£ log2012.log

Description

Since we use gzip-compressed log.tar.gz, we need to add Z to this parameter when looking at the files in the log.tar.gz package.

Example 3: Unzip the TAR package

Command:

Tar-zxvf/opt/soft/test/log.tar.gz

Output:

0 [Email protected] test3]# tar-zxvf/opt/soft/test/log.tar.gzlog2012.log[[email protected] test3]# lslog2012.log[ [Email protected] test3]#

Description

In the case of a preset, we can unzip the file anywhere

Example 4: Extract only some of the files in the/tar

Command:

Tar-zxvf/opt/soft/test/log30.tar.gz Log2013.log

Output:

[Email protected] test]# Tar-zcvf log30.tar.gz log2012.log log2013.log log2012.loglog2013.log[[email protected] test]# ls-Al log30.tar.gz-rw-r--r--1Root root1512  One- -  ,: +log30.tar.gz[[email protected] test]# tar-zxvf log30.tar.gz Log2013.loglog2013.log[[email protected] test]# ll-rw-r--r--1Root root1512  One- -  ,: +Log30.tar.gz[[email protected] test]# CD Test3[[email protected] test3]# tar-zxvf/opt/soft/test/log30.tar.gz Log2013.loglog2013.log[[email protected] test3]# ll Total4-rw-r--r--1Root root A  One- -  .:GenevaLog2013.log[[email protected] test3]#

Description

I can check the file name of the tar package through TAR-ZTVF, if only a single file, you can unzip some files in this way!

Example 5: Files are backed up and their permissions are saved

Command:

TAR-ZCVPF log31.tar.gz log2014.log Log2015.log log2016.log

Output:

[[email protected] test]# ll Total0-rw-r--r--1Root root0  One- -  .:GenevaLog2014.log-rw-r--r--1Root root0  One- -  .: .Log2015.log-rw-r--r--1Root root0  One- -  -: Alog2016.log[[email protected] test]# tar-ZCVPF log31.tar.gz log2014.log log2015.log log2016.log log2014.loglog2015.loglog2016.log[[email protected] test]# CD Test6[[email protected] test6]# ll[[email protected] test6]# tar-zxvpf/opt/soft/test/log31.tar.gz Log2014.loglog2015.loglog2016.log[[email protected] test6]# ll Total0-rw-r--r--1Root root0  One- -  .:GenevaLog2014.log-rw-r--r--1Root root0  One- -  .: .Log2015.log-rw-r--r--1Root root0  One- -  -: ALog2016.log[[email protected] test6]#

Description

The-P property is important, especially if you want to keep the properties of the original file

Example 6: In a folder, a new file is backed up than a date

Command:

Tar-n "2012/11/13"-ZCVF log17.tar.gz test

Output:

[Email protected] soft]# tar-n"2012/11/13"-ZCVF log17.tar.gz testtar:treating Date ' -/ One/ -'As 2012-11-13 00:00:00 + 0 nanosecondsTest/test/log31.tar.gztest/log2014.logtest/linklog.logtest/log2015.logtest/log2013.logtest/log2012.logtest/log2017.logtest/log2016.logtest/log30.tar.gztest/log.tartest/log.tar.bz2test/log.tar.gz

Description

Example 7: Backup folder contents are excluded from some files

Command:

Tar--exclude scf/service-zcvf scf.tar.gz scf/*

Output:

[email protected] test]# tree SCFSCF|-- bin|-- Doc|-- lib '-- service        ' -- deploy                |-- info                '-- product70-- Exclude SCF/SERVICE-ZCVF scf.tar.gz SCF/*  scf/bin/scf/doc/scf/lib/[[email protected] test]#

One Linux command per day (file upload download file operation): "Reprint" tar Command

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.