Linux Common Basic Command tar detailed introduction

Source: Internet
Author: User

"Command Introduction"

tar command You can create 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.

Packing is to turn a large pile of files or directories into a total file;

Compression is to convert a large file into a small file through some compression algorithms.


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.


"Command Function"

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


"Command Format"

tar [Required parameters] [select parameters] [file]


"Command Parameters"

Necessary parameters

-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

Select parameters

-B Set Number of blocks

-c switch to the specified directory

-f Specifies the compressed file

--HELP Display Help information

--version displaying version information


File

File or directory: Specifies a list of files or directories to be packaged.


"Command instance"

Example 1: Package a file into a tar package

[[email protected] test]# ls -lrttotal 24drwxr-xr-x 2 root root  4096 nov 21 17:25 test5drwxr-xr-x 2 root root 4096 nov 21  17:27 test3lrwxrwxrwx 1 root root    7 nov 2206:35  lnlin_soft_link -> lnlinux-rw-r--r-- 2 root root   12  Nov 2206:54 lnlin_hard_link-rw-r--r-- 1 root root    4 nov  2207:09 lnlinuxlrwxrwxrwx 1 root root    5 nov 2207:18  test3_soft_link -> test3-rw-r--r-- 1 root root   12 nov  2423:38 log2013.log-rw-r--r-- 1 root root   10 nov 2423:38  log2015.log[[email protected] test]# tar-cvf log.tar log2015.log  == > Pack-only uncompressed Log2015.log[[emAfter the ail protected] test]# tar-zcvf log.tar.gz log2015.log ==> is packaged, Compress log2015.log[[email protected] test]# tar-jcvf log.tar.bz2 log2015.log == with gzip > After packing, Compress log2015.log[[email protected] test]# ls -lrttotal 44drwxr-xr-x 2 with bzip2  root root  4096 nov 2117:25 test5drwxr-xr-x 2 root root   4096 Nov 2117:27 test3lrwxrwxrwx 1 root root      7 Nov 2206:35 lnlin_soft_link -> lnlinux-rw-r--r-- 2 root  Root    12 nov 2206:54 lnlin_hard_link-rw-r--r-- 1 root root      4 Nov 2207:09 lnlinuxlrwxrwxrwx 1 root root      5 nov 2207:18 test3_soft_link -> test3-rw-r--r-- 1  root root    12 nov 2423:38 log2013.log-rw-r--r-- 1 root root    10  Nov 2423:38 log2015.log-rw-r--r-- 1 root root 10240 Nov 24  23:41 log.tar-rw-r--r-- 1 root root   130 nov 2423:46  log.tar.gz-rw-r--r-- 1 root root   130 nov 2423:47 log.tar.bz2

The file name after option F is taken by yourself, and we are accustomed to using . Tar as an identification. If the z option 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 option is added.

----------------------------------------------------------------------------------------------------

Check which files are in the tar package above:

[Email protected] test]# TAR-ZTVF log.tar.gz-rw-r--r--root/root 102015-11-24 23:38 log2015.log

Since we are using gzip-compressed log.tar.gz, we have to add the z option when checking the files in the log.tar.gz package.

----------------------------------------------------------------------------------------------------

Example 2: Unzip the TAR package

Extract all the files in the tar package

[Email protected] test]# tar-zxvf/root/test/log.tar.gz Log2013.loglog2015.log

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

----------------------------------------------------------------------------------------------------

Extract only some of the files in tar

[Email protected] test]# tar-zxvf/root/test/log.tar.gz log2015.log log2015.log

----------------------------------------------------------------------------------------------------

I can check the file name in the tar package through TAR-ZTVF.

[Email protected] test]# tar-ztvf/root/test/log.tar.gz-rw-r--r--root/root 122015-11-24 23:38 log2013.log-rw-r-- r--root/root 102015-11-24 23:38 log2015.log

----------------------------------------------------------------------------------------------------

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

[Email protected] test]# TAR-ZCVPF log.tar.gz log2013.log log2014.loglog2015.log log2013.loglog2014.loglog2015.log

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


This article is from the "Three 刅 wood" blog, please be sure to keep this source http://lnlinux.blog.51cto.com/1331535/1716921

Linux Common Basic Command tar detailed introduction

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.