目錄
打包與壓縮
zip
zip壓縮一個檔案
zip壓縮資料夾
zip的靜默輸出
zip解壓縮指令(unzip)
tar
gzip
gzip壓縮一個目錄
gzip解壓縮(-d)
bzip2
bzip2解壓縮(-d)
參數
首頁 運維 linux運維 linux檔案打包與壓縮的方法是什麼

linux檔案打包與壓縮的方法是什麼

May 17, 2023 pm 04:56 PM
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檔案打包與壓縮的方法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

vscode 無法安裝擴展 vscode 無法安裝擴展 Apr 15, 2025 pm 07:18 PM

VS Code擴展安裝失敗的原因可能包括:網絡不穩定、權限不足、系統兼容性問題、VS Code版本過舊、殺毒軟件或防火牆干擾。通過檢查網絡連接、權限、日誌文件、更新VS Code、禁用安全軟件以及重啟VS Code或計算機,可以逐步排查和解決問題。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

vscode 可以用於 mac 嗎 vscode 可以用於 mac 嗎 Apr 15, 2025 pm 07:36 PM

VS Code 可以在 Mac 上使用。它具有強大的擴展功能、Git 集成、終端和調試器,同時還提供了豐富的設置選項。但是,對於特別大型項目或專業性較強的開發,VS Code 可能會有性能或功能限制。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

vscode終端使用教程 vscode終端使用教程 Apr 15, 2025 pm 10:09 PM

vscode 內置終端是一個開發工具,允許在編輯器內運行命令和腳本,以簡化開發流程。如何使用 vscode 終端:通過快捷鍵 (Ctrl/Cmd ) 打開終端。輸入命令或運行腳本。使用熱鍵 (如 Ctrl L 清除終端)。更改工作目錄 (如 cd 命令)。高級功能包括調試模式、代碼片段自動補全和交互式命令歷史。

See all articles