#3 Linux 常用压缩命令 CheatSheet

2017-03-22

rar

rar a xxx.rar files...
rar e xxx.rar ./

rar a -p123456 xxx.rar files...
rar e -p123456 xxx.rar ./

rar a -v20m xxx.rar files...

tar

常用的参数说明(grep "^ \-[a-zA-Z0-9]" <(tar --help) | sort):

  • vf 分别表示输出详细信息、指定压缩包名称
  • x 解压
  • c 压缩
  • t 列出压缩包里面的文件列表
  • 压缩文件类型(Linux 下最常用的两种):
  • z:gz
  • j:bz2
# To extract an uncompressed archive:
tar -xvf /path/to/foo.tar

# To create an uncompressed archive:
tar -cvf /path/to/foo.tar /path/to/foo/

# To extract a .gz archive:
tar -xzvf /path/to/foo.tgz

# To create a .gz archive:
tar -czvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .gz archive:
tar -ztvf /path/to/foo.tgz

# To extract a .bz2 archive:
tar -xjvf /path/to/foo.tgz

# To create a .bz2 archive:
tar -cjvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .bz2 archive:
tar -jtvf /path/to/foo.tgz

# To create a .gz archive and exclude all jpg,gif,... from the tgz
tar czvf /path/to/foo.tgz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} /path/to/foo/

# To use parallel (multi-threaded) implementation of compression algorithms:
tar -z ... -> tar -Ipigz ...
tar -j ... -> tar -Ipbzip2 ...
tar -J ... -> tar -Ipixz ...

zip

# Create zip file
zip archive.zip file1 directory/

# To list, test and extract zip archives, see unzip
cheat unzip

# Extract archive
unzip archive.zip

# Windows 下创建的压缩包放到 Ubuntu 下解压可能会有编码问题
unzip archive.zip -O gbk

# Test integrity of archive
unzip -tq archive.zip

# List files and directories in a file
unzip -l archive.zip

7z

sudo apt install p7zip-full
dpkg -L p7zip-full p7zip | grep bin/
# /usr/bin/7z
# /usr/bin/7za
# /usr/bin/7zr
# /usr/bin/p7zip

文档中说是支持 7z,xz,tar,gz,zip,bz2,iso,rpm,deb 等等等等格式,不过没用过。

# 压缩
7z a  xxxx.7z files...
# 解压
7z e  xxxx.7z
# 查看文件列表
7z l  xxxx.7z

参考

  1. man
  2. --help
  3. cheat / tldr

#2 Python 文件压缩方式汇总

2015-12-31
  • zlib — Compression compatible with gzip
  • gzip — Support for gzip files
  • bz2 — Support for bzip2 compression
  • lzma — Compression using the LZMA algorithm
  • zipfile — Work with ZIP archives
  • tarfile — Read and write tar archive files

#1 unzip 压缩工具

2015-01-19
zip -e archive.zip file1.txt file2.txt

unzip -l archive.zip # 列出

# 解压缩
unzip archive.zip
unzip -o archive.zip # 覆盖
unzip archive.zip file1.txt file2.txt -d /path/to/directory/ # 指定文件,指定目录
unzip -P password archive.zip # 有密码的压缩文件