1.将/tmp 打包并使用bzip2压缩
[root@localhost ~]# tar -cjvf ./tmp.tar.bz2 /tmp tar: Removing leading `/' from member names /tmp/ /tmp/man.config1.gz /tmp/man.config /tmp/.ICE-unix/ /tmp/man.config9.gz /tmp/man.config.bz2
打包后也可以使用 gzip 来进行压缩 用 -z 参数替换上例中的 -j 参数就可以了 当然打包压缩后的文件名后缀要更改成 *.tar.gz 因为 bzip2 压缩效果比 gzip 更好,所以我一般是用 -j 参数
tar 在打包时 一般会把绝对路径中的 "/" 去除 也就是输出的第一句 "tar: Removing leading `/' from member names" 提示. 这样做的好处是 可以防止你解一个tar包时,直接覆盖正在使用的文件. 比如你从别的主机打包了一个/etc 目录的tar包到本机, 如果 "/" 没有去除的话, 一解包, 因为是绝对路径会直接覆盖本机的 /etc 目录 ,那就悲剧了. 当然你也可以使用 -P 参数保留这个 "/" 不过你解包时就一定要多加小心了
2.查看刚才打包的文件中有哪些文件
[root@localhost ~]# tar -tjvf tmp.tar.bz2 drwxrwxrwt root/root 0 2013-08-17 14:16 tmp/ -rw-r--r-- root/root 2332 2013-08-17 13:14 tmp/man.config1.gz -rw-r--r-- root/root 4940 2013-08-17 12:37 tmp/man.config drwxrwxrwt root/root 0 2013-08-17 12:36 tmp/.ICE-unix/ -rw-r--r-- root/root 2184 2013-08-17 13:15 tmp/man.config9.gz -rw-r--r-- root/root 2195 2013-08-17 12:37 tmp/man.config.bz2
-t 查看打包文件
3.将打包文件解压到/tmp/tar目录下
[root@localhost ~]# mkdir /tmp/tar [root@localhost ~]# tar -xjvf tmp.tar.bz2 -C /tmp/tar tmp/ tmp/man.config1.gz tmp/man.config tmp/.ICE-unix/ tmp/man.config9.gz tmp/man.config.bz2 [root@localhost ~]# ll /tmp/tar total 4 drwxrwxrwt. 3 root root 4096 Aug 17 14:16 tmp
到此 tar 的基本用法就介绍完了,下面我们介绍几个稍稍高级一点的用法
4.还是刚才那个tmp.tar.bz2包 我只想把其中的 "tmp/man.config.bz2" 解压到当前目录,怎么做?
[root@localhost ~]# ll total 56 -rw-------. 1 root root 1350 Jul 8 22:03 anaconda-ks.cfg -rw-r--r--. 1 root root 24606 Jul 8 22:03 install.log -rw-r--r--. 1 root root 7345 Jul 8 22:01 install.log.syslog -rw-r--r--. 1 root root 9947 Aug 17 18:10 tmp.tar.bz2 [root@localhost ~]# tar -xjvf tmp.tar.bz2 tmp/man.config.bz2 tmp/man.config.bz2 [root@localhost ~]# ll ./tmp total 4 -rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2
可以看到只有 man.config.bz2 被解压出来了
5.打包时不包含某些文件的做法
[root@localhost ~]# ll /tmp total 24 -rw-r--r--. 1 root root 4940 Aug 17 12:37 man.config -rw-r--r--. 1 root root 2332 Aug 17 13:14 man.config1.gz -rw-r--r--. 1 root root 2184 Aug 17 13:15 man.config9.gz -rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2 drwxr-xr-x. 3 root root 4096 Aug 17 18:47 tar -rw-r--r--. 1 root root 0 Aug 17 19:13 tartest.out
本例中 不想包含 tar 这个文件夹, 可以这么做
[root@localhost ~]# tar -cjvf tmp.tar.bz2 --exclude=/tmp/tar /tmp tar: Removing leading `/' from member names /tmp/ /tmp/man.config1.gz /tmp/man.config /tmp/tartest.out /tmp/.ICE-unix/ /tmp/man.config9.gz /tmp/man.config.bz2 [root@localhost ~]# tar -tjvf tmp.tar.bz2 |grep "/$" drwxrwxrwt root/root 0 2013-08-17 19:13 tmp/ drwxrwxrwt root/root 0 2013-08-17 12:36 tmp/.ICE-unix/
可以看到打包后的文件中确实没有 tar 这个目录
6.这次我们要将 /tmp 目录下 8月17日 18:00 以后生成的文件打包,可以这样做
[root@localhost ~]# ll /tmp total 24 -rw-r--r--. 1 root root 4940 Aug 17 12:37 man.config -rw-r--r--. 1 root root 2332 Aug 17 13:14 man.config1.gz -rw-r--r--. 1 root root 2184 Aug 17 13:15 man.config9.gz -rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2 drwxr-xr-x. 3 root root 4096 Aug 17 18:47 tar -rw-r--r--. 1 root root 0 Aug 17 19:13 tartest.out [root@localhost ~]# tar -cjvf tmp.tar.bz2 --newer-mtime="2013-08-17 18:00:00" /tmp tar: Removing leading `/' from member names /tmp/ tar: /tmp/man.config1.gz: file is unchanged; not dumped tar: /tmp/man.config: file is unchanged; not dumped /tmp/tartest.out /tmp/.ICE-unix/ tar: /tmp/man.config9.gz: file is unchanged; not dumped /tmp/tar/ /tmp/tar/tmp/ tar: /tmp/tar/tmp/man.config1.gz: file is unchanged; not dumped tar: /tmp/tar/tmp/man.config: file is unchanged; not dumped /tmp/tar/tmp/.ICE-unix/ tar: /tmp/tar/tmp/man.config9.gz: file is unchanged; not dumped tar: /tmp/tar/tmp/man.config.bz2: file is unchanged; not dumped tar: /tmp/man.config.bz2: file is unchanged; not dumped [root@localhost ~]# tar -tjvf tmp.tar.bz2 drwxrwxrwt root/root 0 2013-08-17 19:42 tmp/ -rw-r--r-- root/root 0 2013-08-17 19:13 tmp/tartest.out drwxrwxrwt root/root 0 2013-08-17 12:36 tmp/.ICE-unix/ drwxr-xr-x root/root 0 2013-08-17 18:47 tmp/tar/ drwxrwxrwt root/root 0 2013-08-17 14:16 tmp/tar/tmp/ drwxrwxrwt root/root 0 2013-08-17 12:36 tmp/tar/tmp/.ICE-unix/
这个功能在备份时比较有用,可以做差异备份,如果用tar来做系统备份的话,为了保证备份资料保持原来的权限与属性,可以加入 -p 参数
转载于:https://blog.51cto.com/yuebaibai222/1284024