Linux压缩与解压

压缩与解压

文章目录

  • 压缩工具
    • gzip
      • gzip [OPTION]... [FILE]...
      • 解压 gzip -d
    • bzip2
      • bizp2 [OPTION]... [FILE]...
      • 解压 bzip2 -d
      • 解压并保留原文件 bzip2 -kd
    • xz
      • xz [OPTION]... [FILE]...
      • 解压 xz -d
      • 解压并保留源文件 xz -kd
    • zip
      • zip filename.zip file1 file2 ...
      • zip filename.zip DIR/*
      • 解压 unzip
    • 归档工具 tar [OPTION...] [FILE]...
      • tar cf rile.tar
      • -tf:不展开归档,直接查看归档了哪些文件
      • -cf:创建指定归档文件
      • -xf:还原指定归档文件或者压缩归档文件
      • -xf file.tar -C 压缩到指定目录
      • -zcf file.tar.gz:归档并调用gzip进行压缩
      • -jcf file.tar.bz2:归档并调用bzip2进行压缩
      • -Jcf file.tar.xz:归档并调用xz进行压缩
      • -zxf file.tar.gz:还原gzip格式的压缩归档文件
      • -jxf file.tar.bz2:还原bizp2格式的归档压缩文件
      • -Jxf file.tar.xz:还原xz格式的归档压缩文件

压缩工具

gzip

gzip [OPTION]… [FILE]…

[root@localhost hzw]# ls
hello
[root@localhost hzw]# gzip hello 
[root@localhost hzw]# ls
hello.gz
[root@localhost hzw]# file hello.gz 
hello.gz: gzip compressed data, was "hello", from Unix, last modified: Thu Sep 12 23:57:34 2019

解压 gzip -d

[root@localhost hzw]# gzip -d hello.gz 
[root@localhost hzw]# ls
hello

bzip2

bizp2 [OPTION]… [FILE]…

[root@localhost hzw]# bzip2 hello 
[root@localhost hzw]# ls
hello.bz2
[root@localhost hzw]# file hello.bz2 
hello.bz2: bzip2 compressed data, block size = 900k
### bizp2 

解压 bzip2 -d

[root@localhost hzw]# bzip2 -d hello.bz2 
[root@localhost hzw]# ls
hello

解压并保留原文件 bzip2 -kd

[root@localhost hzw]# bzip2 hello 
[root@localhost hzw]# bzip2 -kd hello.bz2 
[root@localhost hzw]# ls
hello  hello.bz2

xz

xz [OPTION]… [FILE]…

[root@localhost hzw]# xz hello 
[root@localhost hzw]# ls
hello.xz

解压 xz -d

[root@localhost hzw]# xz -d hello.xz 
[root@localhost hzw]# ls
hello 

解压并保留源文件 xz -kd

[root@localhost hzw]# xz hello 
[root@localhost hzw]# ls
hello.xz
[root@localhost hzw]# xz -kd hello.xz 
[root@localhost hzw]# ls
hello  hello.xz

zip

zip filename.zip file1 file2 …

[root@localhost hzw]# zip hello.zip hello
  adding: hello (stored 0%)
[root@localhost hzw]# ls
hello  hello.zip

zip filename.zip DIR/*

[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# touch hello
[root@localhost ~]# ls
anaconda-ks.cfg  hello
[root@localhost ~]# zip 123.zip ./*
  adding: anaconda-ks.cfg (deflated 36%)
  adding: hello (stored 0%)
[root@localhost ~]# ls
123.zip  anaconda-ks.cfg  hello

解压 unzip

[root@localhost ~]# mkdir a
[root@localhost ~]# ls
123.zip  a  anaconda-ks.cfg  hello
[root@localhost ~]# mv 123.zip a
[root@localhost ~]# cd a
[root@localhost a]# unzip 123.zip 
Archive:  123.zip
  inflating: anaconda-ks.cfg         
 extracting: hello                   
[root@localhost a]# ls
123.zip  anaconda-ks.cfg  hello

归档工具 tar [OPTION…] [FILE]…

tar cf rile.tar

-c:创建归档文件

-f file.tar:指定要操作的归档文件

-x:还原归档

-v:显示归档过程

-p:归档时保留权限信息。只有管理员才有权限用此选项

-C:指定还原归档或解压时的目标目录

-tf:不展开归档,直接查看归档了哪些文件

[root@localhost tmp]# tar tf runtime2.tar.bz2 
world/

-cf:创建指定归档文件

[root@localhost tmp]# mkdir hello
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  yum.log
[root@localhost tmp]# tar cf runtime.tar hello

(创建归档文件runtime 并将目录hello存入runtime)

[root@localhost tmp]# file runtime.tar 
runtime.tar: POSIX tar archive (GNU)
[root@localhost tmp]# mv hello world
(将tmp目录下的hello更名为world)
[root@localhost tmp]# ls
ks-script-Wut_Pw  runtime.tar  world  yum.log

-xf:还原指定归档文件或者压缩归档文件

[root@localhost tmp]# tar xf runtime.tar (还原归档文件runtime)
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime.tar  world  yum.log
(有hello和world说明runtime里的hello被成功还原)
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log
[root@localhost tmp]# rm -rf world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  yum.log
[root@localhost tmp]# tar xf runtime2.tar.bz2 
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log

-xf file.tar -C 压缩到指定目录

[root@localhost tmp]# tar xf runtime2.tar.bz2 -C /tmp/hello/
[root@localhost tmp]# ls hello
world

-zcf file.tar.gz:归档并调用gzip进行压缩

[root@localhost tmp]# tar zcf runtime1.tar.gz world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime.tar  world  yum.log
[root@localhost tmp]# file runtime1.tar.gz 
runtime1.tar.gz: gzip compressed data, from Unix, last modified: Fri Sep 13 03:11:50 2019

-jcf file.tar.bz2:归档并调用bzip2进行压缩

[root@localhost tmp]# tar jcf runtime2.tar.bz2 world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime.tar  world  yum.log
[root@localhost tmp]# file runtime2.tar.bz2 
runtime2.tar.bz2: bzip2 compressed data, block size = 900k

-Jcf file.tar.xz:归档并调用xz进行压缩

[root@localhost tmp]# tar Jcf runtime3.tar.xz world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log
[root@localhost tmp]# file runtime3.tar.xz 
runtime3.tar.xz: XZ compressed data

-zxf file.tar.gz:还原gzip格式的压缩归档文件

[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log
[root@localhost tmp]# rm -rf world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  yum.log
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log

-jxf file.tar.bz2:还原bizp2格式的归档压缩文件

[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log
[root@localhost tmp]# rm -rf world/
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  yum.log
[root@localhost tmp]# tar jxf runtime2.tar.bz2 
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log

-Jxf file.tar.xz:还原xz格式的归档压缩文件

[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log
[root@localhost tmp]# rm -rf world
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  yum.log
[root@localhost tmp]# tar Jxf runtime3.tar.xz 
[root@localhost tmp]# ls
hello  ks-script-Wut_Pw  runtime1.tar.gz  runtime2.tar.bz2  runtime3.tar.xz  runtime.tar  world  yum.log

你可能感兴趣的:(Linux压缩与解压)