Debian系統中常用壓縮與解壓工具及使用方法
Debian系統中處理文件壓縮與解壓的常用工具包括tar(歸檔+多格式壓縮)、zip/unzip(ZIP格式專用)、gzip/gunzip(gzip格式專用)、bzip2/bunzip2(bzip2格式專用)等。以下是各工具的具體用法:
tar是Debian中最常用的歸檔工具,可結合不同壓縮算法(gzip、bzip2、xz)實現壓縮。
tar -cvf archive.tar file1 file2 directory1-c(創建歸檔)、-v(顯示詳細過程)、-f(指定歸檔文件名)。tar -czvf archive.tar.gz /path/to/directory_or_file-z(調用gzip壓縮)。tar -cjvf archive.tar.bz2 /path/to/directory_or_file-j(調用bzip2壓縮)。tar -cJvf archive.tar.xz /path/to/directory_or_file-J(調用xz壓縮)。tar -czvf archive.tar.gz --exclude='*.log' /path/to/directory--exclude(排除匹配模式的文件)。zip是跨平臺常用的壓縮格式,適合壓縮單個文件或目錄。
zip -r archive.zip file1 file2 directory1-r(遞歸壓縮目錄及其內容)。unzip archive.zip(默認解壓到當前目錄);unzip archive.zip -d /path/to/destination(指定解壓目錄)。gzip用于壓縮單個文件(無法直接壓縮目錄,需先用tar打包)。
gzip file.txt → 生成file.txt.gz(原文件被刪除)。gunzip file.txt.gz → 恢復為file.txt(原壓縮文件被刪除)。bzip2壓縮率高于gzip,但速度較慢,同樣需配合tar打包目錄。
bzip2 file.txt → 生成file.txt.bz2(原文件被刪除)。bunzip2 file.txt.bz2 → 恢復為file.txt(原壓縮文件被刪除)。tar -xvf archive.tar-x(解壓)。tar -xzvf archive.tar.gztar -xjvf archive.tar.bz2tar -xJvf archive.tar.xztar -xzvf archive.tar.gz -C /path/to/destination-C(指定解壓目錄)。unzip archive.zip(默認解壓到當前目錄);unzip archive.zip -d /path/to/destination(指定解壓目錄)。gunzip file.gzbunzip2 file.bz2unxz file.xz(需安裝xz-utils包)。.deb是Debian系統的軟件包格式,可使用dpkg或ar命令解壓。
dpkg -x package.deb /path/to/destination(僅提取文件,不安裝);dpkg -e package.deb /path/to/destination(僅提取控制文件,如control、postinst)。ar x package.deb → 生成三個文件:debian-binary(版本信息)、control.tar.gz(控制文件)、data.tar.gz(數據文件)。sudo apt update && sudo apt install tar zip unzip gzip bzip2 xz-utils命令安裝。/usr/local)需用sudo提升權限。xz> bzip2> gzip(壓縮率越高,耗時越長)。-r參數(如tar -czvf archive.tar.gz directory/),否則僅壓縮目錄本身而非內容。