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
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:
[Email protected] test]# Ls-al Log2012.log
---xrw-r--1 root root 302108 11-13 06:03 log2012.log
[Email protected] test]# TAR-CVF Log.tar log2012.log
Log2012.log
[Email protected] test]# TAR-ZCVF log.tar.gz log2012.log
Log2012.log
[Email protected] test]# TAR-JCVF log.tar.bz2 log2012.log
Log2012.log
[Email protected] test]# Ls-al *.tar*
-rw-r--r--1 root root 307200 11-29 17:54 Log.tar
-rw-r--r--1 root root 1413 11-29 17:55 log.tar.bz2
-rw-r--r--1 root root 1413 11-29 17:54 log.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 2012-11-13 06:03:25 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:
[email protected] test3]# LL
Total 0[[email protected] test3]# tar-zxvf/opt/soft/test/log.tar.gz
Log2012.log
[[email protected] test3]# ls
Log2012.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.log
Log2013.log
[Email protected] test]# Ls-al log30.tar.gz
-rw-r--r--1 root root 1512 11-30 08:19 log30.tar.gz
[Email protected] test]# TAR-ZXVF log30.tar.gz log2013.log
Log2013.log
[email protected] test]# LL
-rw-r--r--1 root root 1512 11-30 08:19 log30.tar.gz
[Email protected] test]# CD TEST3
[Email protected] test3]# tar-zxvf/opt/soft/test/log30.tar.gz Log2013.log
Log2013.log
[email protected] test3]# LL
Total 4
-rw-r--r--1 root root 11-13 06:03 Log2013.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
Total 0
-rw-r--r--1 root root 0 11-13 06:03 log2014.log
-rw-r--r--1 root root 0 11-13 06:06 log2015.log
-rw-r--r--1 root root 0 11-16 14:41 log2016.log
[Email protected] test]# TAR-ZCVPF log31.tar.gz log2014.log log2015.log log2016.log
Log2014.log
Log2015.log
Log2016.log
[Email protected] test]# CD TEST6
[email protected] test6]# LL
[Email protected] test6]# tar-zxvpf/opt/soft/test/log31.tar.gz
Log2014.log
Log2015.log
Log2016.log
[email protected] test6]# LL
Total 0
-rw-r--r--1 root root 0 11-13 06:03 log2014.log
-rw-r--r--1 root root 0 11-13 06:06 log2015.log
-rw-r--r--1 root root 0 11-16 14:41 log2016.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 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 contents are excluded from some files
Command:
Tar--exclude scf/service-zcvf scf.tar.gz scf/*
Output:
[Email protected] test]# tree SCF
Scf
|--bin
|--doc
|--Lib
'--Service
'--Deploy
|--Info
'--product
7 directories, 0 files
[Email protected] test]# tar--exclude scf/service-zcvf scf.tar.gz scf/*
scf/bin/
scf/doc/
scf/lib/
[Email protected] test]#
One Linux command per day: Tar command