寫在前面:
博主是一名投身教培事業的標準八零後,叛逆而且追求自由,暱稱取自於蘇軾的《水調歌頭》中的“高處不勝寒”,時刻提醒自己要耐得住寂寞,受的了孤獨,在技術的道路上,不畏艱難,勇往直前。
我已經將全部的linux運維體系整理到了gitee上,https://gitee.com/gaosh08/LinuxFamily
歡迎star,投稿,交流,後續還會有python系列和java系列。
文章目錄
文件的歸檔和壓縮命令有很多,我們今天着重來介紹以下內容:
- tar命令進行文件的歸檔和壓縮
- zip管理壓縮文件
歸檔和壓縮文件的好處:節約硬盤的資源 ,加快文件傳輸速率
我們先來看tar命令
1 . tar: 作用打包壓縮文件
用法: tar 【參數】 file
參數:
參數 | 作用 |
---|---|
-c | create創建文件 |
-x | -extract [ˈekstrækt] 提取 解壓還原文件 |
-v | –verbose顯示執行詳細過程 |
-f | –file指定備份文件 |
-t | –list 列出壓縮包中包括哪些文件,不解包,查看包中的內容 |
-C | (大寫)–directory 指定解壓位置 |
舉例:
1) 把當前的路徑下的文件打包,命名爲loacl.tar
[root@zmgaosh ~]# tar -cvf local.tar ./
[root@zmgaosh ~]# ls
2 a.txt b.txt.patch ***local.tar*** passwd1 zmedu.txt
a.sh b.txt file passwd test zmeduv2.txt
解壓縮:
[root@zmgaosh ~]# mkdir test1
[root@zmgaosh ~]# mv local.tar test1/
[root@zmgaosh ~]# cd test1
[root@zmgaosh test1]# tar xvf local.tar
[root@zmgaosh test1]# ls
2 a.txt b.txt.patch file passwd test zmeduv2.txt
a.sh b.txt etc.tar local.tar passwd1 zmedu.txt
[root@zmgaosh test1]#
2) 指定解壓位置 -C
[root@zmgaosh ~]# ls
2 a.txt b.txt.patch file passwd test zmedu.txt
a.sh b.txt etc.tar local.tar passwd1 test1 zmeduv2.txt
[root@zmgaosh ~]# tar xvf local.tar -C /opt/ #解壓到/opt下
[root@zmgaosh ~]# ls /opt/ #查看是否解壓成功
2 a.sh a.txt b.txt b.txt.patch etc.tar file passwd passwd1 test test1 xinsz1 zmedu.txt zmeduv2.txt
[root@zmgaosh ~]#
3) 不解包查看tar包中的內容
[root@zmgaosh ~]# tar tvf local.tar
dr-xr-x--- root/root 0 2020-06-20 19:57 ./
-rw------- root/root 5461 2020-06-19 20:55 ./.viminfo
-rw-r--r-- root/root 91 2020-06-19 20:17 ./passwd
-rwxrwxrwx root/root 157 2020-06-17 23:22 ./a.sh
-rw-r--r-- root/root 176 2013-12-29 10:26 ./.bashrc
2. tar 歸檔+壓縮
語法: tar czvf newfile.tar.gz
常用參數:
-z, --gzip 以gzip方式壓縮 擴展名: tar.gz
-j : 以bz2方式壓縮的 擴展名:tar.bz2
-J : 以xz 方式壓縮 擴展名:tar.xz
舉例:
1.創建tar.gz的包
[root@zmedu16 ~]# tar cvf etc.tar /etc
[root@localhost test]# tar zcvf etc.tar.gz /etc #歸檔,注意備份的名字後綴
[root@localhost test]# tar zxvf etc.tar.gz #解壓縮
2. 創建tar.bz2包
語法: #tar jcvf newfile.tar.bz2 SOURCE
[root@zmedu16 ~]# tar -jcvf etc.tar.bz2 /etc
[root@zmedu16 ~]# tar -jxvf etc.tar.bz2 /etc #解壓縮
[root@zmedu16 ~]# tar jxvf etc.tar.bz2 -C /opt #解壓到opt目錄下
3. 創建.tar.xz的包
[root@zmedu16 ~]# tar -Jcvf etc.tar.xz /etc
[root@zmedu16 ~]# tar -xvf etc.tar.xz #tar.xz 這類包,解壓縮
或:
[root@zmedu16 ~]# tar -Jxvf etc.tar.xz
三種壓縮方式中, tar.gz tar.bz2是比較常用的
tar.xz 壓縮比例最高,壓縮時間最長,壓縮完文件最小
3. zip 管理壓縮文件
zip是壓縮程序,unzip是解壓程序
安裝zip
[root@zmgaosh zip]# yum install zip
壓縮:
[root@zmgaosh zip]# zip passwd.zip /etc/passwd
adding: etc/passwd (deflated 61%)
[root@zmgaosh zip]# ls
passwd.zip
解壓縮:
[root@zmgaosh zip]# unzip passwd.zip
Archive: passwd.zip
inflating: etc/passwd
[root@zmgaosh zip]# ls
etc passwd.zip
如果要指定目標解壓目錄可以加參數-d
[root@zmgaosh zip]# unzip passwd.zip -d /opt/
小技巧
如何查看看壓縮文件的內容
1. 使用vim直接查看壓縮包的內容
[root@zmgaosh zip]# vim passwd.zip
2. 使用file文件查看
[root@zmgaosh zip]# file passwd.zip
passwd.zip: Zip archive data, at least v2.0 to extract
總結
Linux下壓縮解壓命令還有很多,但最常見的還是tar命令和zip命令
比如:
1) ,gz的包:
解壓:gunzip filename.gz
2).bz2 的包
bzip2 -d filename.bz2
3).tar.bz2的包
tar jxvf filename.tar.bz2
4).bz的包
bzip2 filename.bz
5).Z的包
uncompress filename.Z
6).rar的包
rar x filename.rar
白嫖不好,創作不易,各位的點贊就是勝寒創作的最大動力,我們下篇文章再見!