Linux command daily must learn the tar command _linux shell

Source: Internet
Author: User
Tags bz2 rar unpack

Access to the server through SSH, it is inevitable to use compression, decompression, packaging, unpack, etc., this time the tar command is essential to a powerful tool. The most popular tar in Linux is though small, spite, and powerful.

The tar command creates files for Linux files and directories. With tar, you can create files for a particular file (backup files), change files in your files, or add new files to your files. Tar was originally used to create files on tape, and now users can create files on any device. With the tar command, you can package a large number of files and directories into a single file, which is useful for backing up files or combining several files into a single file for easy network transmission.
The first thing to understand is two concepts: packaging and compression. Packaging refers to a large pile of files or directories into a total file, compression is a large file through some compression algorithm into a small file.

Why should we distinguish between these two concepts? This is because many of the compression programs in Linux can only compress a single file, so that when you want to compress a bunch of files, you have to first punch a stack of files into a package (the tar command), and then compress it (gzip bzip2 command).
The most common packaging program under Linux is tar, and the packages we use in the TAR program are often referred to as the TAR packets, and the TAR package file commands are usually ended with. Tar. After the tar package is generated, it can be compressed with another program.

1. Command format:

tar[necessary parameters [select parameters] [file]

2. Command function:

Used to compress and decompress files. Tar itself does not have a compression function. He's calling the compression function.

3. Command parameters:

The necessary parameters are as follows:
-A new compressed file to existing compression
-B Set Block size
-C To create a new compressed file
Differences between-D record files
-R adding files to already compressed files
-U adds changed and existing files to a compressed file that already exists
-X extracts files from compressed files
-T displays the contents of the compressed file
-Z supports gzip decompression files
-j Support bzip2 Extract files
-Z supports compress decompression files
-V Display Operation procedure
-L File System boundary settings
-K preserves existing files without overwriting
-M retention File not overwritten
-W confirm the correctness of the compressed file

The optional parameters are as follows:

-B Set the number of blocks
-c switch to the specified directory
-F Specify compressed files
--HELP Display Help information
--version Display version Information

4. Common decompression/Compression commands

Tar

Unpack: Tar xvf Filename.tar
Packing: Tar cvf filename.tar dirname
(Note: Tar is packed, 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
Decompression 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
Decompression 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:

[Root@localhost test]# ls-al log2012.log
---xrw-r--1 root 302108 11-13 06:03 log2012.log
[root@localhost te TAR-CVF Log.tar log2012.log 
log2012.log
[root@localhost
test]# tar-zcvf log.tar.gz st]# log2012.log Log2012.log
[root@localhost test]# tar-jcvf log.tar.bz2 log2012.log 
log2012.log
[root@localhost test]# Ls-al *.tar*
-rw-r--r--1 root 307200 11-29 17:54 Log.tar-rw-r--r--
1 root root  1413 11-29 log. tar.bz2
-rw-r--r--1 root  1413 11-29 17:54 log.tar.gz

Description

TAR-CVF Log.tar Log2012.log  only packaged, not compressed! 
TAR-ZCVF log.tar.gz log2012.log  after packaging, gzip compressed 

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

Example 2: Refer to the files in the tar package above

Command:

TAR-ZTVF log.tar.gz

Output:

[Root@localhost test]# TAR-ZTVF log.tar.gz
---xrw-r--root/root 302108 2012-11-13 06:03:25-Log2012.log  

Description

Because we use gzip compressed log.tar.gz, we need to add the Z parameter when we look up the files in the log.tar.gz package.

Example 3: Unzip the TAR package

Command:

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

Output:

[Root@localhost test3]# ll
total 0[root@localhost test3]# tar-zxvf/opt/soft/test/log.tar.gz
log2012.log
[ Root@localhost test3]# ls
log2012.log
[root@localhost test3]#

Description

In a preset scenario, we can undo the compression 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:

[Root@localhost test]# tar-zcvf log30.tar.gz log2012.log log2013.log log2012.log log2013.log
[ Root@localhost test]# ls-al log30.tar.gz 
-rw-r--r--1 root root 1512 11-30 08:19 log30.tar.gz
[root@localhost tes t]# tar-zxvf log30.tar.gz log2013.log
log2013.log
[root@localhost test]# ll
-rw-r--r--1 root root  1512 11-30 08:19 log30.tar.gz
[root@localhost test]# cd test3
[root@localhost test3]# tar-zxvf/opt/soft/test /log30.tar.gz log2013.log
log2013.log
[root@localhost test3]# ll

Total 4

-rw-r--r--1 root 11-13 06:03 log2013.log
[root@localhost test3]#

Description

I can check the file name in the tar package through TAR-ZTVF, if only one file, you can extract some files through this way!

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

Command:

 
  

Output:

[Root@localhost test]# LL

Total 0

-rw-r--r--1 root   0 11-13 06:03 log2014.log
-rw-r--r--1 root 11-13   0 06:06 log2015.log
-rw-r--r --1 root   0 11-16 14:41 log2016.log
[root@localhost test]# tar-zcvpf log31.tar.gz log2014.log log2015.log Lo G2016.log 
log2014.log
log2015.log
log2016.log
[root@localhost test]# cd Test6
[ Root@localhost test6]# ll
[root@localhost test6]# tar-zxvpf/opt/soft/test/log31.tar.gz 
log2014.log
Log2015.log
log2016.log
[root@localhost test6]# ll

Total 0

-rw-r--r--1 root 0 11-13 06:03 log2014.log
-rw-r--r--1 root 0 11-13 06:06 log2015.log
-rw-r--r--1 root 0 11-16 14:41 log2016.log
[Root@localhost test6]#

Description

This-P attribute is important, especially if you want to preserve the properties of the original file

Instance 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:

[Root@localhost soft]# tar-n "2012/11/13"-ZCVF log17.tar.gz test
tar:treating Date ' 2012/11/13 ' as 2012-11-13 00:00:00 + 0 nanoseconds
Test/test/log31.tar.gz
Test/log2014.log
Test/linklog.log
Test/log2015.log
Test/log2013.log
Test/log2012.log
Test/log2017.log
Test/log2016.log
Test/log30.tar.gz
Test/log.tar
test/log.tar.bz2
Test/log.tar.gz

Description

Example 7: Backup folder content is excluded part of file

Command:

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

Output:

[root@localhost test]# tree scf
SCF
| | bin
| | Doc
|-lib
'--Service
    ' Deploy
        | | info
        '-- Product
&NBSP
7 directories, 0 files
[root@localhost test]# tar--exclude scf/service-zcvf scf.tar.gz-scf/*
scf/bin/
scf/doc/
scf/lib/
[root@localhost test]#

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.