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/),否则仅压缩目录本身而非内容。