文件归档, 压缩与解压缩 – tar/gzip/bgzip/gzip2/zip/zstd

归档与压缩

归档:将多个文件归为一个文件,文件总大小不变

压缩:将一个文件压缩,文件总大小减少

Linux环境下常用的命令包括以下几种:

归档

  • tar -cvf / -xvf

压缩

  • gzip/gunzip
  • bgzip
  • bzip2/bunzip2
  • zstd -z / -d :https://github.com/facebook/zstd

归档并压缩

  • tar -cvzf / tar -xvzf
  • zip/unzip

归档与解归档

使用 tar 命令进行归档:

#将directory_to_archive文件夹归档为achived_files.tar
tar -cvf achived_files.tar directory_to_archive/ 

#解归档
tar -xvf achived_files.tar

压缩与解压缩

有多种选择 包括 gzip , bgzip , bzip2 , zstd 等多种算法,使用方法如下:

gzip file.txt
#test.txt     ->  32M
#test.txt.gz  ->  9.9M
gunzip file.txt.gz

bzip2 file.txt
#test.txt     ->  32M
#test.txt.bz2 ->  8.5M
bunzip2 test.txt.bz2

#commly used methods for compressing genome data : block gzip
bgzip file.txt
#test.txt      ->  32M
#test.txt.gz   ->  11M
gunzip file.txt.gz

#plink2 
zstd -z file.txt
#test.txt       -> 32M
#test.txt.zst   -> 11M
zstd -d file.txt.zst

归档+压缩

上面介绍了单独归档或单独压缩的情况,但实际操过过程中我们一般会归档压缩同步进行,

可以使用tar -cvzf与tar -xvzf来压缩与解压缩(相当于用tar先归档然后用gzip压缩),也可以使用zip与unzip(同时完成归档与压缩)

与tar一同是用的常用选项

  • -c: create 创建新的压缩文件
  • -x: extract 解压
  • -v: verbose 输出状态
  • -f: file name 指定文件名
  • -z : gzip
  • -j: bzip2
zip archived_compressed_file file1 file2 ...
unzip archived_compressed_file.zip

tar -cvzf archived_compressed_file.tar.gz directory_to_archive/ 
tar -xvzf archived_compressed_file.tar.gz

tar -jcvf archived_compressed_file.tar.bz2 directory_to_archive/
tar -jxvf archived_compressed_file.tar.bz2

留下评论