Linux命令(5)tar命令 - Go语言中文社区

Linux命令(5)tar命令


tar命令

  • 功能说明:备份文件

  • 用法:tar [选项]… 归档及压缩文件名 FILE...

  • 注意:tar命令选项中可以省略“-”

    选项 作用
    -c 创建.tar格式的归档文件
    -C 展开归档时指定目标文件夹
    -f 表示使用归档文件
    -t 列表查看归档内的文件
    -x 解开.tar格式的包文件
    -z 调用gzip程序进行压缩或解压
    -j 调用bzip2程序进行压缩或解压
    -J 调用xz程序进行压缩或解压
    -r 添加文件到已经归档的文件
    -p 打包时保留文件及目录的权限
    -v 输出详细信息
    --exclude 打包时排除指定的文件,支配通配符
    -X 指定一个文件,将排除的内容写到文件中

文件归档就是把多个文件先打包成一个文件,归档有一个缺点就是不但不会减小体积,还会增大体积,一般来讲,归档以后要使用压缩工具对其执行压缩操作,归档和压缩通常要结合起来使用,归档不会删除原文件的。

示例1:将/etc目录备份为etc.tar文件

[root@node1 ~]# tar cf etc.tar /etc/
tar: Removing leading `/' from member names
[root@node1 ~]# ll -h 
total 28M
-rw-r--r-- 1 root root 28M Feb 25 17:46 etc.tar

示例2:查看etc.tar文件中的列表

[root@node1 ~]# tar tf etc.tar 

示例3:将etc.tar文件释放到当前目录

[root@node1 ~]# tar xf etc.tar
[root@node1 ~]# ls 
etc  etc.tar

示例4:将etc.tar文件释放到/tmp/newtest目录中

tar [选项]… 归档及压缩文件名 [-C 目标目录]

[root@node1 ~]# mkdir /tmp/new
[root@node1 ~]# tar xf etc.tar -C /tmp/new
[root@node1 ~]# ll /tmp/new/
total 12
drwxr-xr-x 74 root root 8192 Feb 25 12:43 etc

示例5:创建5个文件将其打包,并将/etc/passwd添加到打包文件中。

[root@node1 ~]# for i in `seq 5`;do touch $i.txt;done
[root@node1 ~]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  etc  etc.tar
[root@node1 ~]# tar cf test.tar *.txt
[root@node1 ~]# tar tf test.tar 
1.txt
2.txt
3.txt
4.txt
5.txt
5.txt
[root@node1 ~]# tar rf test.tar /etc/passwd
tar: Removing leading `/' from member names
[root@node1 ~]# tar tf test.tar 
1.txt
2.txt
3.txt
4.txt
5.txt
etc/passwd

归档完成后通常需要压缩,结合压缩工具(gzip、bzip2、xz),就能实现压缩多个文件了。

示例6:将/var/log下的所有文件复制到/root目录,并分别使用gzip、bzip2、xz进行归档

[root@node1 ~]# cp -r /var/log/ ./
[root@node1 ~]# du -sh log/
4.1M    log/
[root@node1 ~]# tar zcf log.gz log/	#调用gzip进行压缩
[root@node1 ~]# tar jcf log1.bz2 log/	#调用bzip2进行压缩
[root@node1 ~]# tar Jcf log2.xz log/	#调用xz进行压缩
[root@node1 ~]# du -sh *
4.1M    log
300K    log1.bz2
204K    log2.xz
412K    log.gz

示例7:创建{a-e}5个txt文件,并将其打包,但是打包过程中排除a.txt和b.txt

[root@node1 ~]# mkdir /tmp/test
[root@node1 ~]# cd /tmp/test
[root@node1 test]# for i in {a..e};do touch $i.txt;done
[root@node1 test]# ls
a.txt  b.txt  c.txt  d.txt  e.txt
[root@node1 test]# tar cf test.tar *.txt --exclude=a.txt --exclude=b.txt
[root@node1 test]# tar tf test.tar 
c.txt
d.txt
e.txt

示例8:将root目录下的所有文件打包,但排除mp3文件。

[root@node1 test]# for i in `seq 10`;do touch $i.mp3;done
[root@node1 test]# ls
10.mp3  1.mp3  2.mp3  3.mp3  4.mp3  5.mp3  6.mp3  7.mp3  8.mp3  9.mp3  a.txt  b.txt  c.txt  d.txt  e.txt
[root@node1 test]# tar cf text.tar *.txt --exclude=*.mp3
[root@node1 test]# tar tf text.tar 
a.txt
b.txt
c.txt
d.txt
e.txt

示例9:将示例8中的mp3文件打包,但排除1.mp3、3.mp3、5.mp3

[root@node1 test]# cat >>exclude.txt<<EOF
> 1.mp3
> 3.mp3
> 5.mp3
> EOF

[root@node1 test]# tar cf mp3.tar *.mp3 -X exclude.txt 
[root@node1 test]# tar tf mp3.tar 
10.mp3
2.mp3
4.mp3
6.mp3
7.mp3
8.mp3
9.mp3
版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/shijunxian/p/12907678.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-15 18:39:28
  • 阅读 ( 1392 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢