linux文件打包與壓縮的方法是什么

打包和壓縮

將文件或文件夾合并成一個(gè)包,然后通過壓縮算法進(jìn)行數(shù)據(jù)壓縮,減小包的體積,方便網(wǎng)絡(luò)傳輸。

windows:   zip   rar  linux:   zip   tar   gz   bz2   tar.gz   tar.bz2  壓縮算法:   gzip   bzip2

zip

是一個(gè)Windows和Linux中常用打包壓縮工具,支持的壓縮算法是zip。

zip工具需要安裝   yum install zip

zip壓縮一個(gè)文件

# 格式   zip [參數(shù)] 壓縮包名稱  文件路徑  [root@abc ~]# zip 123.zip 123.log    adding: 123.log (deflated 87%) [root@abc ~]# ls -l

zip壓縮文件夾

# 需要一個(gè)-r參數(shù)去遞歸壓縮文件夾下的所有內(nèi)容 [root@abc ~]# zip -r dir.zip dir/   adding: dir/ (stored 0%)   adding: dir/one/ (stored 0%)   adding: dir/123.log (deflated 87%)

zip的靜默輸出

# -q:參數(shù)就是不輸出任何打包信息 [root@abc opt]# zip -r -q etc.zip /etc/ [root@abc opt]# ls -l

zip解壓命令(unzip)

# 格式   unzip [參數(shù)] 壓縮包路徑  # unzip解壓命令只能解壓由zip打包的壓縮文件 [root@abc ~]# unzip dir.zip  Archive:  dir.zip   inflating: dir/123.log              [root@abc ~]#   # 其他壓縮包由unzip解壓時(shí)隨即報(bào)錯(cuò)。 [root@abc opt]# unzip nginx-.tar.gz Archive:  nginx-.tar.gz   End-of-central-directory signature not found.  Either this file is not   a zipfile, or it constitutes one disk of a multi-part archive.  In the   latter case the central directory and zipfile comment will be found on   the last disk(s) of this archive. unzip:  cannot find zipfile directory in one of nginx-.tar.gz or         nginx-.tar.gz.zip, and cannot find nginx-.tar.gz.ZIP, period.   # 查看壓縮包中壓縮那些內(nèi)容,不解壓? # 只查看壓縮包內(nèi)容不解壓需要使用 -l 參數(shù) [root@abc opt]# unzip -l dir.zip  Archive:  dir.zip   Length      Date    Time    Name ---------  ---------- -----   ----         0  03-11-2021 12:04   dir/ ---------                     -------         0                     1 file  # 解壓到指定目錄(-d) [root@abc ~]# unzip -d /root/  etc.zip  [root@abc opt]# cd /root/ [root@abc ~]# ls ]        anaconda-ks.cfg  dir.zip  index.html           test.pdf.gz  xxxeth0xxx           系統(tǒng)優(yōu)化.md 123.log  demo.txt         etc      nginx-0.1.22.tar.gz  test.txt     上傳與下載.md 123.zip  dir              eth0xxx  test                 xxxeth0      文件管理_(高級(jí)).pdf  # 靜默輸出(-q) [root@abc ~]# rm -rf etc [root@abc ~]# unzip -q -d /root/ /opt/etc.zip  [root@abc ~]# ls -l

tar

tar壓縮支持多種壓縮算法

tar.gz gzip (用的最多)

tar.bz2 bzip2

gzip

通過gzip壓縮算法,將文件壓縮一定體積,有利于傳輸, 不支持打包

[root@abc ~]# ls -l total 4828 -rw-r--r--   1 root root  244977 Mar 10 12:12 index.html [root@abc ~]# gzip index.html  [root@abc ~]# ls -l
gzip壓縮一個(gè)目錄
[root@abc etc]# gzip -r /etc [root@abc etc]# ls

gzip解壓(-d)

[root@abc ~]# ls -l -rw-r--r--   1 0 0   22652 Mar 10 12:12 index.html.gz [root@abc ~]# gzip -d index.html.gz  [root@abc ~]# ls -l

bzip2

使用bzip2 壓縮算法來壓縮一定體積的文件。

[root@abc ~]# ls -l total 4828 -rw-r--r--   1 root root  646165 Mar  9 10:31 123.log      [root@abc ~]# bzip2 123.log  [root@abc ~]# ls -l total 4240 -rw-r--r--   1 root root       0 Mar 10 12:04 ]

bzip2解壓(-d)

bzip2解壓是針對(duì)于bzip2壓縮的壓縮包來進(jìn)行解壓。

[root@abc ~]# ls -l total 4240 -rw-r--r--   1 root root   42210 Mar  9 10:31 123.log.bz2 [root@abc ~]# bzip2 -d 123.log.bz2  [root@abc ~]# ls -l

tar

tar其實(shí)是一個(gè)打包工具,不具備壓縮功能,但是可以使用參數(shù)調(diào)用壓縮工具來進(jìn)行解壓。

tar參數(shù)   -c : 創(chuàng)建壓縮   -f ; 指定壓縮包名稱   -z : 使用gzip壓縮工具進(jìn)行壓縮   -j : 使用bzip2壓縮工具進(jìn)行壓縮   -J : 使用xz壓縮工具進(jìn)行壓縮   -t : 顯示壓縮包內(nèi)容,不解壓   -v : 顯示壓縮過程   -P : 允許使用絕對(duì)路徑進(jìn)行壓縮   -x : 解壓   -C : 指定解壓路徑   -h : 打包軟連接   --exclude : 排除某些文件   --exclude-from :
參數(shù)
  • -c : 創(chuàng)建壓縮包

  • -f : 指定壓縮包名稱

[root@abc ~]# tar -c -f test.tar 123.log  [root@abc ~]# ls -l
  • -z : 指定使用gzip壓縮工具進(jìn)行壓縮

[root@abc ~]# tar  -c -z -f test-one.tar 123.log  [root@abc ~]# ls -l  total 5084 -rw-r--r--   1 root root   85279 Mar 11 15:56 test-one.tar  # 注:使用-z參數(shù),不會(huì)自動(dòng)添加.gz后綴  [root@abc ~]# tar -c -z -f anaconda.tar.gz  anaconda-ks.cfg  [root@abc ~]# ls -l
  • -j : 指定使用bzip2壓縮工具進(jìn)行壓縮

[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log  [root@abc ~]# ls -l
  • -J : 指定使用xz壓縮工具進(jìn)行壓縮

[root@abc test-tar]# tar -c -J  -f etc.tar.xz /etc/ [root@abc ~]# ls -l
  • -t : 查看壓縮包內(nèi)容

[root@abc ~]# tar -t -f 123-bak.tar.bz2  123.log [root@abc ~]#
  • -v : 顯示壓縮包壓縮過程

[root@abc ~]# tar -x -v -f etc.tar -C /opt/
  • -P : 允許使用絕對(duì)路徑進(jìn)行打包

[root@abc ~]# tar -c -P -f 123-three.tar /etc/passwd [root@abc ~]# tar -c -f 123-three.tar /etc/passwd tar: Removing leading `/' from member names [root@abc ~]#
  • -x : 解壓

# tar解壓是按照原來的路徑進(jìn)行解壓 [root@abc test]# tar -x -f etc.tar   # tar會(huì)自動(dòng)識(shí)別壓縮功能
  • -C : 指定解壓路徑

[root@abc ~]# tar -x -f etc.tar -C /opt/ tar: Removing leading `/' from member names [root@abc ~]# cd /opt/ [root@abc opt]# ls abc23  dir  dir.zip  etc  nginx-0.1.22.tar.gz  nginx-.tar.gz  xxx [root@abc opt]#
  • –exclude : 排除某些文件

[root@abc test-tar]# tar -c -f abc.tar ./* --exclude=abc7 --exclude=abc5   --exclude=abc1  [root@abc test-tar]# tar -t -f abc.tar  ./abc2 ./abc3 ./abc4 ./abc6 ./abc8 ./abc9 [root@abc test-tar]#
  • –exclude-from : 根據(jù)某個(gè)文件列表排除多個(gè)文件

[root@abc test-tar]# cat list.txt  abc995 abc996 abc997 abc998 abc999 [root@abc test-tar]# tar -c -f abc.tar ./* --exclude-from=list.txt
  • -h : 打包軟連接

[root@abc test-tar]# tar -c -h -f bin-h.tar /bin

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊10 分享