linux基础命令(28) tar命令
5.使用实例 实例1:将文件全部打包成tar包 命令: tar -cvf log.tar log2012.log tar -zcvf log.tar.gz log2012.log tar -jcvf log.tar.bz2 log2012.log 输出: [root@localhost test]# ls -al log2012.log ---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log [root@localhost test]# tar -cvf log.tar log2012.log log2012.log [root@localhost test]# tar -zcvf log.tar.gz 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 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 说明: 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 输出: [root@localhost test]# tar -ztvf log.tar.gz ---xrw-r-- root/root 302108 2012-11-13 06:03:25 log2012.log 说明: 由于我们使用 gzip 压缩的log.tar.gz,所以要查阅log.tar.gz包内的文件时,就得要加上 z 这个参数了。 实 例3:将tar 包解压缩 命令: tar -zxvf /opt/soft/test/log.tar.gz 输出: [root@localhost test3]# ll 总计 0[root@localhost test3]# tar -zxvf /opt/soft/test/log.tar.gz log2012.log [root@localhost test3]# ls log2012.log [root@localhost test3]# 说明: 在预设的情况下,我 们可以将压缩档在任何地方解开的 实例4:只将 /tar 内的 部分文件解压出来 命令: tar -zxvf /opt/soft/test/log30.tar.gz log2013.log 输出: [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 test]# 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 总计 4 - rw-r--r-- 1 root root 61 11-13 06:03 log2013.log [root@localhost test3]# 说明: 我可以透过 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]# (编辑:应用网_丽江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |