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運維 核心系統命令實踐》的學習筆記。
