Usage of tar command in linux

The most commonly used package program under linux is tar. The package produced by the tar program is often called a tar package. The commands of a tar package file usually end with .tar. After the tar package is generated, it can be compressed by other programs.

1. Command format:

tar[required parameter][optional parameter][file] 

2. Command function:

Used to compress and decompress files. tar itself has no compression capabilities. He is implemented by calling the compression function 

3. Command parameters:

The necessary parameters are as follows:

-A add archive to existing archive

-B set block size

-c create a new compressed file

-d log file differences

-r add files to already compressed files

-u add changed and existing files to existing archives

-x extract files from compressed files

-t show the contents of the compressed file

-z support gzip decompressing files

-j supports bzip2 decompression files

-Z supports compress to decompress files

-v show the operation process

-l filesystem boundary setting

-k keep the original file without overwriting

-m keep files from being overwritten

-W confirms the correctness of the compressed file

The optional parameters are as follows:

-b sets the number of blocks

-C switch to the specified directory

-f specifies the compressed file

--help display help information

--version display version information

4. Common decompression/compression commands

tar 
解包:tar xvf FileName.tar
打包:tar cvf FileName.tar DirName

(Note: tar is packaging, not compression!)

.gz
decompress 1: gunzip FileName.gz
decompress 2: gzip -d FileName.gz
compress: gzip FileName

.tar.gz 和 .tgz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName
.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName

.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName
.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知

.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知
.Z
解压:uncompress FileName.Z
压缩:compress FileName

.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName

.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName
.rar
解压:rar x FileName.rar
压缩:rar a FileName.rar DirName 

 

5.使用实例

实例1:将文件全部打包成tar包

命令:


输出:



说明:

tar -cvf log.tar log2012.log    仅打包,不压缩! 

tar -zcvf log.tar.gz log2012.log   打包后,以 gzip 压缩 
tar -zcvf log.tar.bz2 log2012.log  打包后,以 bzip2 压缩 

在参数 f 之后的文件档名是自己取的,我们习惯上都用 .tar 来作为辨识。 如果加 z 参数,则以 .tar.gz 或 .tgz 来代表 gzip 压缩过的 tar包; 如果加 j 参数,则以 .tar.bz2 来作为tar包名。


实例2:查阅上述 tar包内有哪些文件

命令:

tar -ztvf log.tar.gz

输出:


说明:

由于我们使用 gzip 压缩的log.tar.gz,所以要查阅log.tar.gz包内的文件时,就得要加上 z 这个参数了。

实例3:将tar 包解压缩

命令:

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

输出:


说明:

在预设的情况下,我们可以将压缩档在任何地方解开的

实例4:只将 /tar 内的 部分文件解压出来

命令:


输出:


说明:

我可以透过 tar -ztvf 来查阅 tar 包内的文件名称,如果单只要一个文件,就可以透过这个方式来解压部分文件!

实例5:文件备份下来,并且保存其权限

命令:

复制代码
代码如下:

tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log 

输出:

复制代码
代码如下:

[root@localhost test]# ll
总计 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
[root@localhost test]# tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.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
总计 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
[root@localhost test6]#

说明:

这个 -p 的属性是很重要的,尤其是当您要保留原本文件的属性时

实例6:在 文件夹当中,比某个日期新的文件才备份

命令:


复制代码
代码如下:

tar -N "2012/11/13" -zcvf log17.tar.gz test

输出:


复制代码
代码如下:

[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

说明:

实例7:备份文件夹内容是排除部分文件

命令:

复制代码
代码如下:

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

输出:

复制代码
代码如下:

[root@localhost test]# tree scf
scf
|-- bin
|-- doc
|-- lib
`-- service
`-- deploy
|-- info
`-- product
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]#</p> <p>


【实例】

1.将文件解压到指定目录下

命令:tar zxvf test.tgz -C 指定目录

如:tar zxvf /usr/local/jdk-8u161-linux-x64.tar.gz -C /usr/local/jdk1.8  

     将压缩文件jdk-8u161-linux-x64.tar.gz解压到/usr/local/jdk1.8目录下






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326198824&siteId=291194637