
2. 易于管理 (批量删除文件)
如图:主机A要跟主机B传输一个大小为10G的文件估计传送100s.

如果直接传输会大量的占用流量带宽.导致公司的内网访问速度缓慢.
我把10G的文件压缩成5G,传送时间是50s.
文件压缩的好处:
节约硬盘的资源.
加快文件传输的速率.
tar 文件.是把几个文件和(或)目录集合在一个文件夹里。是创建备份和归档的最佳工具
[root@linuxidc ~]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
Examples:
tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. 创建存档
tar -tvf archive.tar # List all files in archive.tar verbosely.列出所有文件
tar -xf archive.tar # Extract all files from archive.tar. 从archive.tar提取所有文件。
# c create 创建
#v 详细
#f filename
[root@linuxidc~]# tar tvf aa.tar #查看包里的文件
[root@linuxidc ~]# tar xf aa.tar #解包
linux对于文件的扩展名没有像windows要求的那么严格,所以在使用linux的过程中经常会遇到有些文件根本就没有扩展名,哪么我们应该如何去判断没有扩展名的文件,到底是文件还是目录呢?
作用:确定文件类型
语法:file 文件名
[root@linuxidc ~]# touch a.txt
[root@linuxidc ~]# file a.txt
a.txt: empty
[root@linuxidc ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@linuxidc ~]# file /home/berners
/home/berners: directory
[root@linuxidc ~]# file /etc/init.d/network
/etc/init.d/network: Bourne-Again shell script, ASCII text executable
[root@linuxidc ~]#

例:把两个目录或目标+文件打包成一个软件包
30 15 13 jun mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1。3.tar -jcv -f /root/etc.newer.then.passwd.tar.bz2 --newer-mtime="2016/10/18" /etc*备份。12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&152 5 * * * root /usr/local/src/analog-5.32-lh/analog >> /dev/null 2>&1。
[root@linuxidc ~]# tar tvf bb.tar
操作-解包:
[root@localhost ~]# tar xvf bb.tar #将bb.tar 解压
注意: 解包过程中如果不指定解包路径,那么会按照原路径解包,会覆盖原文件,这一点要特别小心,尤其是原路径下的文件在打包后修改过。
操作-解压指定路径:
-C #指定解压的路径位置
[root@linuxidc ~]# tar xvf aa.tar -C /home/berners/ #将bb.tar 解压到/home/berners目录下
[root@linuxidc ~]# ls

aa.tar Documents kernel.txt Pictures Templates
anaconda-ks.cfg Downloads Music Public Videos
Desktop initial-setup-ks.cfg passwd.txt redhat.txt who.out
[root@linuxidc ~]# tar xf aa.tar -C /home/berners
[root@linuxidc ~]# ls /home/berners
a.out touch.txt who.out
[root@linuxidc ~]#
操作-对比文件的大小:
[root@linuxidc ~]# du -sh aa.tar
20K aa.tar
[root@linuxidc ~]# ll -h /home/berners/aa.tar
-rw-r--r-- 1 root root 20K 8月 14 11:22 /home/berners/aa.tar
[root@linuxidc ~]#
gzip bzip2 zip tar
一、压缩格式:gz, bz2, xz, zip, Z
格式(文件名格式): .tar.gz 或 .tgz
语法格式:tar zcvf newfile.tar.gz SOURCE
tar -zcvf /home/abc.tar.gz /home/abc 打包,并用gzip压缩。[12:07:40]root@u2015:/home/nagios# tar zxvfnagios-4.0.7.tar.gz。[14:16:35]root@u2015:/home/nagios# tar zxvfnagios-plugins-2.0.3.tar.gz。
[root@linuxidc ~]# ll -h aa.tar*
-rw-r--r-- 1 root root 20K 8月 14 10:37 aa.tar //压缩前20k。
[root@server share]# tar zcvf commands.tar.gz .commands[root@server share]# rm -rf commands.tar.gz。30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz/etc >> /dev/null 2>&1 它将在6月13日周一的15:30运行 tar czf/usr/local/backups/daily/etc.tar.gz /etc 命令。[root@172 ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz[root@172 ~]# tar -zxvf pcre-8.39.tar.gz。
用tar zcf压缩文件, 压缩前(20k)/压缩后(3.7k) = 5.4 倍。
可能你要说我的文件比较小,那我来弄一个大的文件比较一下:
[root@linuxidc ~]# du -sh share.tar*
1.2G share.tar
457M share.tar.gz
[root@linuxidc ~]#
这个文件比较大,压缩前和压缩后比率为 1.2*1024/457 =2.69 倍。
结论:tar zcf 这个压缩比率还是很低的。而且文件越大,越缩比率越低。
语法格式:tar zxvf xx.tar.gz .
v可以省略,v用来表示操作过程是否显示。
[root@linuxidc ~]# tar zxf aa.tar.gz -C /home/lisi
[root@linuxidc ~]# ls -l /home/berners
total 36
-rw-r--r-- 1 root root 20480 8月 14 11:22 aa.tar
-rwxr-xr-x 1 root root 8547 8月 13 19:46 a.out
-rw-r--r-- 1 dabai xiaobai 0 8月 10 07:27 touch.txt
-rw-r--r-- 1 root root 143 8月 13 12:23 who.out
[root@linuxidc ~]# ls -l /home/lisi
total 0
drwxr-xr-x 3 root root 20 8月 14 13:31 home
[root@linuxidc ~]# cd home
[root@linuxidc home]# ls
berners
[root@linuxidc home]# cd berners
[root@linuxidc berners]# ls -l
total 36

-rw-r--r-- 1 root root 20480 8月 14 11:22 aa.tar
-rwxr-xr-x 1 root root 8547 8月 13 19:46 a.out
-rw-r--r-- 1 dabai xiaobai 0 8月 10 07:27 touch.txt
-rw-r--r-- 1 root root 143 8月 13 12:23 who.out
格式(文件名格式): .tar.bz2
语法格式:tar jcvf newfile.tar.bz2 SOURCE
压缩文件
[root@localhost ~]# zip passwd.zip /etc/passwd
-r 压缩目录
格式”zip 选项 名称 源”
[root@localhost ~]# zip -r grub2.zip /boot/grub2/
[root@localhost ~]# ll -h grub2.*
-rw-r--r-- 1 root root 7.7M Feb 17 07:40 grub2.tar
root@tecmint:~# tar -jxvf abc.tar.bz2 (记住'j'代表了.tar.bz2)。4.tar -jtv -f /root/etc.newer.then.passwd.tar.bz2 | grep -v ‘/$‘:显示tar.bz2内的结尾非/的文件名。tar -jtv -f /root/filename.tar.bz2 | grep ‘记录在filename.tar.bz2内的文件名(关键字即可)‘。
-rw-r--r-- 1 root root 3.1M Feb 17 07:56 grub2.tar.gz
-rw-r--r-- 1 root root 3.2M Feb 17 08:11 grub2.zip
[root@localhost ~]# unzip grub2.zip -d /opt/
-d指定路径
Linux下如何解压.tar.bz2格式的压缩包 老版本的linux要两步,一步是解压缩,一步是解包
bzip2 -d **.tar.bz2 //将文件解压成**.tar
tar -xf **.tar //解包
现在新出的linux版本,只要一步就可以解压完毕
tar -xf **.tar.bz2
linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的。生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲tar命令的基本用法:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-113117-1.html
进入一级战备状态以捍卫国家主权
演员杨洋
错了应该是世界第一
≧▽≦)