每天一个Linux命令(24)tar命令
tar命令可以为linux的文件和目录创建档案。
(1)用法:
用法: tar [选项] [文件参数]
(2)功能:
功能: 用来压缩和解压文件。tar本身不具有压缩功能。它是调用压缩功能实现的。
利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。
要弄清两个概念:打包和压缩。
打包是指将一大堆文件或目录变成一个总的文件;压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
为什么要区分这两个概念呢?
这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆文件时,你得先将这一大堆文件先打成一个包(tar命令),然后再用压缩程序进行压缩(gzip bzip2命令)。
(3)选项参数:
1) -c --create 建立新的备份文件
2) -z 支持gzip解压文件
3) -j 支持bzip2解压文件
4) -v --verbose 显示指令执行过程
5) -f<备份文件> --file=<备份文件> 指定备份文件
6) -t或--list 列出备份文件的内容
7) -N<日期格式>--newer=<日期时间> 只将较指定日期更新的文件保存到备份文件里
8) -x或--extract或--get 从备份文件中还原文件
9) -C <目录> 这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项
10) -P --absolute-names 文件名使用绝对名称,不移除文件名称前的“/”号
(4)实例:
1)[root@localhost Documents]# tar -cvf Dir.tar Dir f选项参数是必不可少的,这里以普通压缩格式压缩文件夹
[root@localhost Documents]# tar -cvf Dir.tar Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
总用量
-rwx--xrwx. root root 5月 : core.log
dr-xr-xr-x. root root 5月 : Dir
-rw-r--r--. root root 5月 : Dir.tar
-r--r--r--. root root 5月 : find
dr--r--r--. root root 5月 : findDir
-r--r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : tail_text
--w-------. root root 5月 : tempory
--w-------. root root 5月 : uText
2)[root@localhost Documents]# tar -czvf Dir.tar.gz Dir 以其他格式压缩有Gzip和bzip2两种格式
[root@localhost Documents]# tar -czvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -cjvf Dir.tar.bz2 Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
总用量
-rwx--xrwx. root root 5月 : core.log
dr-xr-xr-x. root root 5月 : Dir
-rw-r--r--. root root 5月 : Dir.tar
-rw-r--r--. root root 5月 : Dir.tar.bz2 //公认的后缀名,以bzip2压缩
-rw-r--r--. root root 5月 : Dir.tar.gz //公认的后缀名,以Gzip压缩
-r--r--r--. root root 5月 : find
dr--r--r--. root root 5月 : findDir
-r--r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : tail_text
--w-------. root root 5月 : tempory
--w-------. root root 5月 : uText
-rw-r--r--. root root 5月 : vf
3)[root@localhost Documents]# tar -ztvf Dir.tar.gz 查阅上述tar包内有哪些文件,按什么格式压缩就要按照什么格式解压
[root@localhost Documents]# tar -ztvf Dir.tar.gz
dr-xr-xr-x root/root -- : Dir/
-r-xr-xr-x root/root -- : Dir/head_text
-r-xr-xr-x root/root -- : Dir/less1
-r-xr-xr-x root/root -- : Dir/less2
[root@localhost Documents]# tar -jtvf Dir.tar.bz2
dr-xr-xr-x root/root -- : Dir/
-r-xr-xr-x root/root -- : Dir/head_text
-r-xr-xr-x root/root -- : Dir/less1
-r-xr-xr-x root/root -- : Dir/less2
4)[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir 解压tar.gz压缩包,到指定名的文件夹,必须是Dir,不能变
[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost findDir]# ll
总用量
dr-xr-xr-x. root root 5月 : Dir
-rw-r--r--. root root 5月 : Dir.tar
-rw-r--r--. root root 5月 : Dir.tar.bz2
-rw-r--r--. root root 5月 : Dir.tar.gz
-r--r--r--. root root 5月 : p1.pdf
-r--r--r--. root root 5月 : p2.pdf
-r--r--r--. root root 5月 : t1.txt
-r--r--r--. root root 5月 : T1.txt
-r--r--r--. root root 5月 : t2.txt
-r--r--r--. root root 5月 : T2.txt
5)[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1 只将tar内的部分文件解压出来,要进行匹配,如果不匹配会报错“归档找不到”
[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1
Dir/less1
6)[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText 文件备份下来,并且保存其权限
[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText
find
t3.txt
vf
uText
7)[root@localhost Documents]# tar -pzxvf P.tar.gz -C Pdir 指定解压的目录
[root@localhost Documents]# tar -zcvf P1.tar.gz find t3.txt vf uText //没有p参数的打包并压缩
find
t3.txt
vf
uText
[root@localhost Documents]# ll
总用量
-rwx--xrwx. root root 5月 : core.log
dr-xr-xr-x. root root 5月 : Dir
-r--r--r--. root root 5月 : find
dr--r--r--. root root 5月 : findDir
-r--r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : P1.tar.gz //没有p参数
-rw-r--r--. root root 5月 : P.tar.gz //有p参数
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : tail_text
--w-------. root root 5月 : tempory
--w-------. root root 5月 : uText
-rw-r--r--. root root 5月 : vf
[root@localhost Documents]# mkdir Pdir
[root@localhost Documents]# tar -zxvf P.tar.gz -C Pdir //指定解压缩的路径
find
t3.txt
vf
uText
[root@localhost Documents]# mkdir NoPdir
[root@localhost Documents]# tar -zxvf P1.tar.gz -C NoPdir
find
t3.txt
vf
uText
[root@localhost Documents]# ls -l Pdir //查看有p无p的区别
总用量
-r--r--r--. root root 5月 : find
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : uText
-rw-r--r--. root root 5月 : vf
[root@localhost Documents]# ls -l NoPdir
总用量
-r--r--r--. root root 5月 : find
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : uText
-rw-r--r--. root root 5月 : vf //并没有发现有什么区别
8)[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./* 在文件夹当中,比某个日期新的文件才备份
[root@localhost Documents]# tar -N "2016/05/20" -zcvf .tar.gz ./*
tar: 选项 --after-date: 将日期 ‘2016/05/20’ 当作 2016-05-20 00:00:00
./520.tar.gz
tar: ./520.tar.gz:文件缩小 45 字节;用零填充
./core.log
./Dir/
./Dir/head_text
./Dir/less1
./Dir/less2
./find
./findDir/
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
./findDir/t2.txt
./findDir/Dir.tar
./findDir/Dir.tar.gz
./findDir/Dir.tar.bz2
./findDir/Dir/
./findDir/Dir/head_text
./findDir/Dir/less2
./findDir/Dir/less1
./log17.tar.gz
./newlocate
./NoPdir/
./NoPdir/find
./NoPdir/t3.txt
./NoPdir/vf
./NoPdir/uText
./P1.tar.gz
./Pdir/
./Pdir/find
./Pdir/t3.txt
./Pdir/vf
./Pdir/uText
./P.tar.gz
tar: ./t3.txt: 文件未改变;未输出
tar: ./tail_text: 文件未改变;未输出
tar: ./tempory: 文件未改变;未输出
tar: ./uText: 文件未改变;未输出
./vf
9)[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir 打包时不包括特定目录下的文件
[root@localhost Documents]# tar --exclude ./Dir/less1 -zcvf Dir.test.tar.gz Dir //这里目录加个./,经后续验证,没有达到不包括的效果
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test.tar.gz
dr-xr-xr-x root/root -- : Dir/
-r-xr-xr-x root/root -- : Dir/head_text
-r-xr-xr-x root/root -- : Dir/less1
-r-xr-xr-x root/root -- : Dir/less2
[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir
Dir/
Dir/head_text
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test2.tar.gz
dr-xr-xr-x root/root -- : Dir/
-r-xr-xr-x root/root -- : Dir/head_text
-r-xr-xr-x root/root -- : Dir/less2
(5)其他:
利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件。tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案。
每天一个Linux命令(24)tar命令的更多相关文章
- linux基础-第八单元 正文处理命令及tar命令
第八单元 正文处理命令及tar命令 使用cat命令进行文件的纵向合并 两种文件的纵向合并方法 归档文件和归档技术 归档的目的 什么是归档 tar命令的功能 tar命令的常用选项 使用tar命令创建.查 ...
- linux中的 tar命令的 -C 参数,以及其它一些参数(转)
linux中的 tar命令的 -C 参数,以及其它一些参数 复制源:http://www.cnblogs.com/li-hao/archive/2011/10/03/2198480.htmltar命令 ...
- Linux压缩打包tar命令总结
命令简介 在Linux系统的维护.管理中,tar命令是一个使用频率很高的命令,tar命令的功能主要是将众多文件打包成一个tar文件并压缩,并且能保持文件的权限属性.tar其实最开始是用来做磁带 ...
- linux中的 tar命令的 -C 参数,以及其它一些参数
tar命令的-C参数 $ tar -cvf file2.tar /home/usr2/file2 tar: Removing leading '/' from members names hom ...
- Linux基础(3)- 正文处理命令及tar命令、vi编辑器、硬盘分区、格式化及文件系统的管理和软连接、硬连接
一.正文处理命令及tar命令 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件1.txt(覆盖) 2) 将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件2.txt(追加) ...
- Linux命令学习-tar命令
Linux中,tar命令的全称是tape archive,主要作用是压缩和解压文件. 参数说明: -c 创建新的压缩档案 -x 解压档案 -t 列出压缩档案的内容 -z 使用gzip来解压和压缩,文件 ...
- 每天一个linux命令(27)--tar命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候 tar 命令就是必不可少的一个功能强大的工具.Linux 中最流行的 tar 是麻雀虽小,五脏俱全. tar 命令可以为Linux ...
- Linux使用快捷键,who命令,rm命令,ps命令,cd,命令kill命令,find命令,grep命令,tar命令(gz、tar、bz2),用户管理,vim配置的一部分,相关命令
1.进入Ubuntu开场后的终端窗口的快捷键是: ctrl + alt+t:通过这个命令能够打开终端. ctrl + alt+t:通过这个命令能够打开终端. 再开一个tab选项卡式 ...
- Linux 正文处理命令及tar命令 利用vi编辑器创建和编辑正文文件
要点回顾 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cp /etc/passwd . cat ./passwd >1.txt cp /etc/group ...
- linux下使用tar命令
解压语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用. 主选项: c 创建新的档案文件.如果用户想备份一个目录或 ...
随机推荐
- 【HTML5】元素<script>与<noscript>的使用
功能描述 在新建的页面中增加一个文本框"txtContent"和一个按钮"请点击我":当单击按钮时.通过页面中加入的JavaScript脚本获取为文本框中的内容 ...
- 个人shell积累
1:bc 计算器2:ps:列出用户的进程3:more 有三种用法 $more filename 显示文件内容 $command | more 将command命令的输出分页显示 $more < ...
- C#IAsyncResult异步回调函数的解释
问题:IAsyncResult ar 是如何通过ar.AsyncState强制转换成TCPClientState类型 答:实例中使用的方法如下 我给IAsyncResult ar传入了TCPClien ...
- Spring4整合Hibernate5时不能自动生成表结构
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: Spring4整合Hibernate5时,不再使用hibernate.cfg.xml,将其内容整合到Spring配置文件中,启动后不能 ...
- Android IntentService全然解析 当Service遇到Handler
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...
- NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(三)
Precondition: hadoop 2.7.1 hbase 0.98.13 solr 5.2.1 / Apache Solr 4.8.1 http://archive.apache.org ...
- 在Ubuntu 16.04下安装 virtualbox 5.2
sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" ...
- 如何玩转最新的项目的搭配springmvc+mybatis+Redis+Nginx+tomcat+mysql
上一次完成nginx+tomcat组合搭配,今天我们就说说,这几个软件在项目中充当的角色: 要想完成这几个软件的组合,我们必须知道和熟悉应用这个框架, 一: Nginx:在项目中大多数作为反向代理服务 ...
- shell 遍历所有文件包括子目录
1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error : syntax er ...
- web开发之html5---html5 动画特效舞动的雨伞
http://www.cnblogs.com/stoneniqiu/p/4199294.html