[linux]按部就班學運維-基礎命令篇-文件的歸檔和壓縮

寫在前面:
博主是一名投身教培事業的標準八零後,叛逆並且追求自由,暱稱取自於蘇軾的《水調歌頭》中的「高處不勝寒」,時刻提醒本身要耐得住寂寞,受的了孤獨,在技術的道路上,不畏艱難,一往無前。
我已經將所有的linux運維體系整理到了gitee上,https://gitee.com/gaosh08/LinuxFamily
歡迎star,投稿,交流,後續還會有python系列和java系列。java


文章目錄


文件的歸檔和壓縮命令有不少,咱們今天着重來介紹如下內容:python


  • tar命令進行文件的歸檔和壓縮
  • zip管理壓縮文件

歸檔和壓縮文件的好處:節約硬盤的資源 ,加快文件傳輸速率linux

咱們先來看tar命令git

1 . tar: 做用打包壓縮文件

用法: tar 【參數】 filevim

參數:bash

參數 做用
-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 ~]# ls2     a.txt  b.txt.patch  ***local.tar***  passwd1  zmedu.txt
a.sh  b.txt  file         passwd     test     zmeduv2.txt

解壓縮:ide

[root@zmgaosh ~]# mkdir test1[root@zmgaosh ~]# mv local.tar test1/[root@zmgaosh ~]# cd test1[root@zmgaosh test1]# tar xvf local.tar [root@zmgaosh test1]# ls2     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 ~]# ls2     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.gzlinux運維

經常使用參數:spa

-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]# lspasswd.zip

解壓縮:

[root@zmgaosh zip]# unzip passwd.zip Archive:  passwd.zip
  inflating: etc/passwd              
[root@zmgaosh zip]# lsetc  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

白嫖很差,創做不易,各位的點贊就是勝寒創做的最大動力,咱們下篇文章再見!

相關文章
相關標籤/搜索