共計 4385 個字符,預計需要花費 11 分鐘才能閱讀完成。
本篇內容主要講解“linux 文件打包與壓縮的方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“linux 文件打包與壓縮的方法是什么”吧!
打包和壓縮
將文件或文件夾合并成一個包,然后通過壓縮算法進行數據壓縮,減小包的體積,方便網絡傳輸。
windows: zip
rar
linux:
zip
tar
gz
bz2
tar.gz
tar.bz2
壓縮算法: gzip
bzip2
zip
是一個 Windows 和 Linux 中常用打包壓縮工具,支持的壓縮算法是 zip。
zip 工具需要安裝
yum install zip
zip 壓縮一個文件
# 格式
zip [參數] 壓縮包名稱 文件路徑
[root@abc ~]# zip 123.zip 123.log
adding: 123.log (deflated 87%)
[root@abc ~]# ls -l
zip 壓縮文件夾
# 需要一個 - r 參數去遞歸壓縮文件夾下的所有內容
[root@abc ~]# zip -r dir.zip dir/
adding: dir/ (stored 0%)
adding: dir/one/ (stored 0%)
adding: dir/123.log (deflated 87%)
zip 的靜默輸出
# -q:參數就是不輸出任何打包信息
[root@abc opt]# zip -r -q etc.zip /etc/
[root@abc opt]# ls -l
zip 解壓命令(unzip)
# 格式
unzip [參數] 壓縮包路徑
# unzip 解壓命令只能解壓由 zip 打包的壓縮文件
[root@abc ~]# unzip dir.zip
Archive: dir.zip
inflating: dir/123.log
[root@abc ~]#
# 其他壓縮包由 unzip 解壓時隨即報錯。[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.
# 查看壓縮包中壓縮那些內容,不解壓?# 只查看壓縮包內容不解壓需要使用 -l 參數
[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 系統優化.md
123.log demo.txt etc nginx-0.1.22.tar.gz test.txt 上傳與下載.md
123.zip dir eth0xxx test xxxeth0 文件管理_(高級).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 壓縮一個目錄
[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 解壓是針對于 bzip2 壓縮的壓縮包來進行解壓。
[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 其實是一個打包工具,不具備壓縮功能,但是可以使用參數調用壓縮工具來進行解壓。
tar 參數
-c : 創建壓縮
-f ; 指定壓縮包名稱
-z : 使用 gzip 壓縮工具進行壓縮
-j : 使用 bzip2 壓縮工具進行壓縮
-J : 使用 xz 壓縮工具進行壓縮
-t : 顯示壓縮包內容,不解壓
-v : 顯示壓縮過程
-P : 允許使用絕對路徑進行壓縮
-x : 解壓
-C : 指定解壓路徑
-h : 打包軟連接
--exclude : 排除某些文件
--exclude-from :
參數
-c : 創建壓縮包
-f : 指定壓縮包名稱
[root@abc ~]# tar -c -f test.tar 123.log
[root@abc ~]# ls -l
-z:指定使用 gzip 壓縮工具進行壓縮
[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 參數,不會自動添加.gz 后綴
[root@abc ~]# tar -c -z -f anaconda.tar.gz anaconda-ks.cfg
[root@abc ~]# ls -l
-j : 指定使用 bzip2 壓縮工具進行壓縮
[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log
[root@abc ~]# ls -l
-J : 指定使用 xz 壓縮工具進行壓縮
[root@abc test-tar]# tar -c -J -f etc.tar.xz /etc/
[root@abc ~]# ls -l
-t : 查看壓縮包內容
[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 : 允許使用絕對路徑進行打包
[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 解壓是按照原來的路徑進行解壓
[root@abc test]# tar -x -f etc.tar
# tar 會自動識別壓縮功能
-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 : 根據某個文件列表排除多個文件
[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
到此,相信大家對“linux 文件打包與壓縮的方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!