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

简介: Linux 系统环境文件打包、解包、压缩、解压缩
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.

目录
相关文章
|
2天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
2天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
3 0
|
7天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
16 0
|
9天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
82 0
|
3月前
|
Linux
百度搜索:蓝易云【Linux中如何对文件进行压缩和解压缩?】
这些是在Linux中进行文件压缩和解压缩的常见方法。根据您的需求和具体情况,可能会使用其他压缩工具和选项。您可以通过查阅相应命令的帮助文档来获取更多详细信息。
53 1
|
4月前
|
NoSQL Java Linux
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)
|
4月前
|
算法 Java Linux
Linux下文件增删改查定位压缩操作与权限所属用户
Linux下文件增删改查定位压缩操作与权限所属用户
41 0
|
6月前
26Linux - 文件管理(文件压缩解压:bzip2)
26Linux - 文件管理(文件压缩解压:bzip2)
31 0
|
4月前
|
Java Shell Linux
Linux【脚本 01】简单Shell脚本实现定时备份文件、压缩、删除超时文件操作(showDoc文件备份脚本举例)
Linux【脚本 01】简单Shell脚本实现定时备份文件、压缩、删除超时文件操作(showDoc文件备份脚本举例)
107 0
|
1月前
|
Unix Linux Shell
Linux 常用命令汇总(一):查询帮助 & 文件压缩及解压缩 & 搜索文件
Linux 常用命令汇总(一):查询帮助 & 文件压缩及解压缩 & 搜索文件
157 2