如何在 Unix 命令列上壓縮/解壓檔案
介紹
在 Linux 中,壓縮和解壓檔案是一種非常常見的操作。出於以下原因,我們需要將許多檔案壓縮成一個檔案。
它節省了系統的磁碟空間。
我們可以將多個檔案儲存在一個檔案中。這也有助於將此壓縮檔案複製到另一個系統。
當我們擁有一個壓縮檔案時,我們也應該知道如何解壓它以獲取所有所需的檔案。因此,壓縮和解壓命令在 Linux 中非常重要。在本文中,我們將學習各種壓縮和解壓命令。
使用基本的 zip 和 unzip 命令
首先,Linux 系統中應該存在“zip”和“unzip”命令。如果這些命令不存在,請使用以下命令安裝。
$ sudo apt install zip $ sudo apt install unzip
現在,讓我們建立 4 個文字檔案。
$ touch 1.txt 2.txt 3.txt 4.txt $ ls 1.txt 2.txt 3.txt 4.txt
將所有 4 個檔案壓縮到一個名為“all”的檔案中
$ zip all *.txt adding: 1.txt (stored 0%) adding: 2.txt (stored 0%) adding: 3.txt (stored 0%) adding: 4.txt (stored 0%)
我們可以看到建立了 all.zip 檔案,其中包含 1.txt、2.txt、3.txt、4.txt。
$ ls 1.txt 2.txt 3.txt 4.txt all.zip
現在讓我們刪除所有 4 個檔案並解壓 all.zip。我們可以看到所有 4 個文字檔案。
$ rm *.txt $ unzip all.zip Archive: all.zip extracting: 1.txt extracting: 2.txt extracting: 3.txt extracting: 4.txt $ ls 1.txt 2.txt 3.txt 4.txt all.zip
將檔案解壓到目標目錄
在前面的示例中,我們已經看到所有文字檔案都解壓到當前路徑。建議將檔案解壓到不同的目錄,以便解壓的檔案不會與當前路徑中的其他檔案混淆。
我們可以使用以下命令將“all.zip”解壓到目錄“new-all”
$ unzip -q all.zip -d new-all $ ls new-all/ 1.txt 2.txt 3.txt 4.txt
壓縮時排除特定檔案
在某些情況下,如果我們需要排除某些檔案進行壓縮,可以使用以下命令。
$ zip all 1.txt 2.txt 3.txt -x 4.txt updating: 1.txt (stored 0%) updating: 2.txt (stored 0%) updating: 3.txt (stored 0%)
從輸出中,我們可以看到 all.zip 中不包含 4.txt。
讓我們解壓“all.zip”。
$ unzip -q all.zip -d 4.txt-not-there $ ls 4.txt-not-there/ 1.txt 2.txt 3.txt
從上面的輸出中,我們可以看到“4.txt”不存在。
在不解壓的情況下查詢壓縮檔案中的所有檔案
在某些情況下,如果我們需要檢視壓縮檔案中有哪些檔案,可以使用以下命令。
$ unzip -l all.zip Archive: all.zip Length Date Time Name --------- ---------- ----- ---- 0 2016-02-11 22:25 1.txt 0 2016-02-11 22:25 2.txt 0 2016-02-11 22:25 3.txt --------- ------- 0 3 files
使用密碼保護您的壓縮檔案
在某些情況下,如果我們需要使用密碼保護壓縮檔案,可以使用以下命令。
$ zip -e -r password-protected new-all/ Enter password: Verify password: adding: new-all/ (stored 0%) adding: new-all/4.txt (stored 0%) adding: new-all/2.txt (stored 0%) adding: new-all/3.txt (stored 0%) adding: new-all/1.txt (stored 0%)
我們可以看到系統提示使用者輸入密碼,並建立了“password-protected.zip”。
$ ls 1.txt 2.txt 3.txt 4.txt 4.txt-not-there all.zip new-all password-protected.zip
現在讓我們使用正確的密碼解壓“password-protected.zip”。
$ unzip password-protected.zip Archive: password-protected.zip [password-protected.zip] new-all/4.txt password: replace new-all/4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/4.txt replace new-all/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/2.txt replace new-all/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/3.txt replace new-all/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y extracting: new-all/1.txt
現在嘗試使用錯誤的密碼執行相同的“unzip”命令。
$ unzip password-protected.zip Archive: password-protected.zip [password-protected.zip] new-all/4.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/4.txt incorrect password [password-protected.zip] new-all/2.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/2.txt incorrect password [password-protected.zip] new-all/3.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/3.txt incorrect password [password-protected.zip] new-all/1.txt password: password incorrect--reenter: password incorrect--reenter: skipping: new-all/1.txt incorrect password
我們可以看到它沒有成功。
結論
透過本文,我們學習瞭如何使用“zip”和“unzip”命令。還學習了使用各種引數執行密碼保護的壓縮檔案、將壓縮檔案解壓到目標目錄以及在壓縮時排除某些檔案。