1.tar:打包备份
该命令是将多个命令打包到一起,并且可以实现解压打包。打包是将多个文件或者目录变成一个总的文件,压缩则是将一个大的文件通过压缩算法变成一个小文件。
参数 | 说明 |
z(常用) | 通过gzip压缩或解压缩,一般都是以tar.gz结尾 |
j(常用) | 通过bzip2压缩和解压缩 一般都是以tar.bz2结尾 |
c(常用) | 创建新的tar包 |
v(常用) | 显示详细的tar命令执行过程 |
f(常用) | 指定压缩文件的名字,组合中一般f放到最后,因为f后面要立即接文件名 |
x(常用) | 解开tar包 |
C(常用) | 指定解压的目录路径 |
示例:
1)备份站点目录
[root@boxiaoyuan ~]# mkdir -p /var/www/html/boxiaoyuan/test [root@boxiaoyuan ~]# touch /var/www/html/{1..10}.html [root@boxiaoyuan ~]# ls /var/www/html 10.html 2.html 4.html 6.html 8.html boxiaoyuan 1.html 3.html 5.html 7.html 9.html [root@boxiaoyuan ~]# cd /var/www/ [root@boxiaoyuan www]# cd html/ [root@boxiaoyuan html]# ls 10.html 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html [root@boxiaoyuan html]# cd .. [root@boxiaoyuan www]# tar zcvf www.tar.gz ./html/ # linux中一般都是以tar.gz结尾 ./html/ ./html/7.html ./html/4.html ./html/6.html ./html/3.html ./html/1.html ./html/9.html ./html/8.html ./html/2.html ./html/5.html ./html/10.html
2)查看压缩的内容
[root@boxiaoyuan www]# tar ztvf www.tar.gz drwxr-xr-x root/root 0 2019-04-16 19:42 ./html/ -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/7.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/4.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/6.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/3.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/1.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/9.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/8.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/2.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/5.html -rw-r--r-- root/root 0 2019-04-16 19:40 ./html/10.html
3)解压缩文件
[root@boxiaoyuan www]# tar zxvf www.tar.gz ./html/ ./html/7.html ./html/4.html ./html/6.html ./html/3.html ./html/1.html ./html/9.html ./html/8.html ./html/2.html ./html/5.html ./html/10.html
3.zip:打包和压缩文件
该命令可以对文件进行压缩,与gzip相比,zip命令压缩文件不仅不会删除源文件,还可以压缩目录。
参数 | 说明 |
-r(常用) | 将指定目录下的所有文件和子目录一并压缩,递归压缩 |
-x(常用) | 压缩文件时排除某个文件 |
示例:
1)压缩文件
[root@boxiaoyuan html]# cd /tmp [root@boxiaoyuan tmp]# cp /etc/services . [root@boxiaoyuan tmp]# ls -alh services* -rw-r--r--. 1 root root 626K 4月 17 11:58 services [root@boxiaoyuan tmp]# zip service.zip ./services adding: services (deflated 80%) [root@boxiaoyuan tmp]# ls -alh service* -rw-r--r--. 1 root root 626K 4月 17 11:58 services -rw-r--r--. 1 root root 125K 4月 17 12:00 service.zip
2)压缩目录
[root@boxiaoyuan /]# zip -r tmp.zip ./tmp/ adding: tmp/ (stored 0%)
4.unzip:解压zip文件
该命令可以解压缩zip命令或者其他压缩软件压缩的zip格式的文件。
参数选项 | 解释说明 |
-l | 不解压显示压缩包文件列表 |
-o | 解压时不提示是否覆盖文件 |
-d | 指定解压目录 |
-v | 解压时显示信息信息 |
示例:
1)常规解压文件
[root@boxiaoyuan /]# unzip tmp.zip Archive: tmp.zip replace tmp/.X0-lock? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: tmp/.X0-lock replace tmp/orbit-root/bonobo-activation-server-479f6d31a65026d71b1617890000c716-ior? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
解压时如果不想要提示是否覆盖,可以使用unzip -o tmp.zip命令。
2)不解压显示文件内容-l选项
[root@boxiaoyuan /]# unzip -l tmp.zip Archive: tmp.zip Length Date Time Name --------- ---------- ----- ---- 0 04-17-2019 12:00 tmp/ 0 04-13-2019 16:56 tmp/jetty-0.0.0.0-8089-war-_-any-3391660028267644253.dir/ 0 04-16-2019 16:20 tmp/orbit-gdm/ 0 04-12-2019 09:03 tmp/keyring-E58AUd/ 11 04-15-2019 14:00 tmp/.X0-lock 0 04-17-2019 08:59 tmp/orbit-root/ tmp/winstone5092352002510277416.jar --------- ------- 94522804 7 files
注:本文内容为《跟老男孩学linux运维 核心系统命令实践》的学习笔记。