实例 : Linux系统 centos8 文件打包、解包、压缩、解压缩

1. gz文件类型:

解压:

[arthur@localhost Documents]$ gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
  -d, --decompress  decompress
...
  -1, --fast        compress faster
  -9, --best        compress better

[arthur@localhost Documents]$ ll
total 484
drwxrwxr-x. 2 arthur arthur   4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur     42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur     20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 487578 Sep  3 09:30 sample.reads_for.fastq.gz
drwxrwxr-x. 2 arthur arthur     41 Apr 15 09:41 script
[arthur@localhost Documents]$ gzip -d sample.reads_for.fastq.gz 
[arthur@localhost Documents]$ ll
total 1988
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_for.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
2. bz2文件类型
[arthur@localhost Documents]$ bzip2 --help
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
 ...
   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

[arthur@localhost Documents]$ ll
total 1824
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_for.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
#压缩
[arthur@localhost Documents]$ bzip2 sample.reads_for.fastq 
[arthur@localhost Documents]$ ll
total 320
drwxrwxr-x. 2 arthur arthur   4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur     42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur     20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 322712 Sep  3 09:30 sample.reads_for.fastq.bz2
drwxrwxr-x. 2 arthur arthur     41 Apr 15 09:41 script
#解压
[arthur@localhost Documents]$ bunzip2 sample.reads_for.fastq.bz2 
[arthur@localhost Documents]$ ll
total 1824
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_for.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
#压缩: (添加 管道 命令)
[arthur@localhost Documents]$ bzip2 sample.reads_for.fastq  | ll
total 1824
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_for.fastq
-rw-------. 1 arthur arthur       0 Sep  3 09:38 sample.reads_for.fastq.bz2
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
[arthur@localhost Documents]$ ll
total 320
drwxrwxr-x. 2 arthur arthur   4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur     42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur     20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 322712 Sep  3 09:30 sample.reads_for.fastq.bz2
drwxrwxr-x. 2 arthur arthur     41 Apr 15 09:41 script
#解压:(添加 管道  命令)
[arthur@localhost Documents]$ bzip2 -d sample.reads_for.fastq.bz2  | ll
total 320
drwxrwxr-x. 2 arthur arthur   4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur     42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur     20 Apr 18 23:12 notes
-rw-------. 1 arthur arthur      0 Sep  3 09:39 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 322712 Sep  3 09:30 sample.reads_for.fastq.bz2
drwxrwxr-x. 2 arthur arthur     41 Apr 15 09:41 script
[arthur@localhost Documents]$ ll
total 1824
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_for.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
3. xz文件类型:
[arthur@localhost Documents]$ xz --help 
Usage: xz [OPTION]... [FILE]...
Compress or decompress FILEs in the .xz format.

  -z, --compress      force compression
  -d, --decompress    force decompression
  -t, --test          test compressed file integrity
  -l, --list          list information about .xz files
4. tar.gztar.bz2文件类型:
[arthur@localhost Documents]$ tar --help 
Usage: tar [OPTION...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

########################################################################
文件打包与解包
#查看当前目录与文件 
[arthur@localhost Documents]$ ll
total 5472
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
#将文件 for 和 rev 打包为 seq.tar
[arthur@localhost Documents]$ tar -cf seq.tar sample.reads_for.fastq  sample.reads_rev.fastq && ll
total 9112
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
-rw-rw-r--. 1 arthur arthur 3727360 Sep  3 09:54 seq.tar
#查看包内文件:
[arthur@localhost Documents]$ tar -tvf seq.tar 
-rwxrwxr-x arthur/arthur 1861842 2021-09-03 09:53 sample.reads_for.fastq
-rwxrwxr-x arthur/arthur 1861842 2021-09-03 09:30 sample.reads_rev.fastq
#解开包并将包内文件输出到新的路径下
[arthur@localhost Documents]$ tar -xf seq.tar  -C ../Desktop/
[arthur@localhost Documents]$ ll ../Desktop/ 
total 5420
-rw-rw-r--. 1 arthur arthur  162525 Apr 14 11:37 20210414log
-rw-rw-r--. 1 arthur arthur 1441063 Apr 15 02:44 20210415.log
-rw-rw-r--. 1 arthur arthur   46384 Apr 18 10:25 20210418log
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
[arthur@localhost Documents]$ ll
total 7284
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
-rw-rw-r--. 1 arthur arthur 3727360 Sep  3 09:54 seq.tar
########################################################################
文件打包压缩与文件解包解压缩
tar 可以使用 -z 参数来调用gzip进行压缩;使用 -j 参数来调用 bzip2 进行压缩;
tar 调用 compress , compress 也是一个压缩程序, `.Z`结尾的文件就是 bzip2 压缩的结果。相对应解压程序为 uncompress. tar 中使用 -Z  参数调用 compress 

#文件打包压缩为 seq.tar.gz,  参数 `-Z`
#此处输出文件命名不规范,应为  seq.tar.Z
[arthur@localhost Documents]$ tar -Zcf seq.tar.gz sample.reads_for.fastq  sample.reads_rev.fastq 
[arthur@localhost Documents]$ ll
total 8108
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
-rw-rw-r--. 1 arthur arthur 3727360 Sep  3 09:54 seq.tar
-rw-rw-r--. 1 arthur arthur  841611 Sep  3 10:16 seq.tar.gz
#文件打包压缩为 seq2.tar.gz,  参数 `z`
[arthur@localhost Documents]$ tar -zcf seq2.tar.gz sample.reads_for.fastq  sample.reads_rev.fastq 
[arthur@localhost Documents]$ ll
total 9060
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
-rw-rw-r--. 1 arthur arthur  974587 Sep  3 10:19 seq2.tar.gz
-rw-rw-r--. 1 arthur arthur 3727360 Sep  3 09:54 seq.tar
-rw-rw-r--. 1 arthur arthur  841611 Sep  3 10:16 seq.tar.gz
#创建目录存放解包解压缩文件
[arthur@localhost Documents]$ mkdir seq2 seq1
[arthur@localhost Documents]$ ll
total 9060
drwxrwxr-x. 2 arthur arthur    4096 Sep  3 08:50 blog
drwxrwxr-x. 2 arthur arthur      42 Apr  7 04:11 log
drwxr-xr-x. 2 arthur arthur      20 Apr 18 23:12 notes
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:53 sample.reads_for.fastq
-rwxrwxr-x. 1 arthur arthur 1861842 Sep  3 09:30 sample.reads_rev.fastq
drwxrwxr-x. 2 arthur arthur      41 Apr 15 09:41 script
drwxrwxr-x. 2 arthur arthur       6 Sep  3 10:21 seq1
drwxrwxr-x. 2 arthur arthur       6 Sep  3 10:21 seq2
-rw-rw-r--. 1 arthur arthur  974587 Sep  3 10:19 seq2.tar.gz
-rw-rw-r--. 1 arthur arthur 3727360 Sep  3 09:54 seq.tar
-rw-rw-r--. 1 arthur arthur  841611 Sep  3 10:16 seq.tar.gz
#
[arthur@localhost seq2]$ tar -zxvf ../seq2.tar.gz 
sample.reads_for.fastq
sample.reads_rev.fastq
[arthur@localhost seq2]$ cd ../
[arthur@localhost Documents]$ cd seq1
#
[arthur@localhost seq1]$ tar -Zxvf ../seq.tar.gz  
sample.reads_for.fastq
sample.reads_rev.fastq
5. 解压缩文件总结:
  1. 对于.tar结尾的文件:

    • tar -xf file.tar
  2. 对于.gz结尾的文件:
    -gzip -d file.gz
    -gunzip file.gz
  3. 对于.tgz.tar.gz结尾的文件
    -tar -zxvf file.tar.gz
    -tar -zxvf file.tgz
  4. 对于.bz2结尾的文件:
    -bzip2 -d file.bz2
    -bunzip2 file.bz2
  5. 对于tar.bz2结尾的文件:
    -tar -jxvf file.tar.bz2
  6. 对于.Z结尾的文件:
    -uncompress file.Z
  7. 对于.tar.Z结尾的文件:
    -tar -xZf file.tar.Z

reference :
[1] 鸟哥私房菜
[2] 解压缩、压缩命令.runoob.

上一篇:Android 中屏蔽 Home 键


下一篇:改善Java代码质量的