实例 : 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.

目录
相关文章
|
25天前
|
Linux
在Linux中,列出几种常见打包工具并写相应解压缩参数。
在Linux中,列出几种常见打包工具并写相应解压缩参数。
|
2月前
|
监控 安全 Linux
Linux命令rpmkeys:守护RPM包的安全之门
`rpmkeys`是Linux中管理RPM包公钥的工具,用于验证软件包的签名,确保安全。它支持导入、导出、删除和查询公钥,通过数字签名保证包的完整性和来源可靠性。例如,使用`--import`导入公钥,`--query`查询公钥。最佳实践包括验证公钥来源、定期更新并备份。该命令间接保障了数据处理和分析的安全环境。
|
25天前
|
机器学习/深度学习 Ubuntu Linux
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
在Linux中,如何按照该要求抓包:只过滤出访问http服务的,目标ip为192.168.0.111,一共抓1000个包,并且保存到1.cap文件中?
|
26天前
|
算法 Linux 数据安全/隐私保护
“Linux压缩大师”:gzip、bzip2、tar与zip
在Linux系统管理中,文件压缩与解压至关重要,能有效减少存储空间占用并加快文件传输。常用工具包括gzip、bzip2、tar和zip。gzip采用Lempel-Ziv算法,压缩率高且速度快,适用于单个文件压缩,扩展名为.gz。bzip2压缩率更高但速度稍慢,同样用于单个文件,扩展名为.bz2。tar主要用于打包文件而不直接压缩,常与gzip或bzip2结合使用实现压缩打包。zip则是一种通用压缩工具,支持多文件压缩及密码保护,兼容性好。这些工具让Linux环境下的文件管理更加高效便捷。
18 1
|
27天前
|
Ubuntu Linux
在Linux中,如何升级系统内所有已安装软件包?
在Linux中,如何升级系统内所有已安装软件包?
|
1月前
|
存储 Linux Windows
Linux zip命令:压缩文件或目录
我们经常会在 Windows 系统上使用 “.zip”格式压缩文件,其实“.zip”格式文件是 Windows 和 Linux 系统都通用的压缩文件类型,属于几种主流的压缩格式(zip、rar等)之一,是一种相当简单的分别压缩每个文件的存储格式,本节要讲的 zip 命令,类似于 Windows 系统中的 winzip 压缩程序,其基本格式如下: [root@localhost ~]#zip [选项] 压缩包名 源文件或源目录列表 注意,zip 压缩命令需要手工指定压缩之后的压缩包名,注意写清楚扩展名,以便解压缩时使用。 下面给大家举几个例子。 【例 1】zip 命令的基本使用。 [r
43 0
Linux zip命令:压缩文件或目录
|
1月前
|
存储 Linux
Linux专栏08:Linux基本指令之压缩解压缩指令
Linux专栏08:Linux基本指令之压缩解压缩指令
36 4
|
2月前
|
运维 监控 网络协议
Linux抓包命令tcpdump使用技巧大全
【7月更文挑战第10天】
72 5
Linux抓包命令tcpdump使用技巧大全
|
24天前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
24天前
|
Linux C# C++
【Azure App Service For Container】创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务
【Azure App Service For Container】创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务