加入收藏 | 设为首页 | 会员中心 | 我要投稿 佛山站长网 (https://www.0757zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux中tar命令使用详解

发布时间:2016-11-04 04:39:50 所属栏目:Linux 来源:网络整理
导读:tar 用来将多个文件或目录打包成一个大文件 1.将/tmp 打包并使用bzip2压缩 [root@localhost ~]# tar -cjvf ./tmp.tar.bz2 /tmp tar: Removing leading `/' from
副标题[/!--empirenews.page--]

tar

用来将多个文件或目录打包成一个大文件

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

-c 建立打包文件

-j 使用 bzip2 对打包后的文件进行压缩

-v 在打包,解包过程中,显示正在处理的文件或目录

-f 后面接打包后新生成的打包文件的文件名

打包后也可以使用 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

-x 解包打包文件

-C 将打包文件解压到指定目录下, 如果不加这个参数, 默认是会解压到当前工作目录下

到此 tar 的基本用法就介绍完了,下面我们介绍几个稍稍高级一点的用法

(编辑:佛山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读