linux---压缩与解压缩命令

时间:2021-09-14 18:49:52

常用压缩格式: .zip .gz .bz2

  1. .zip 格式压缩
    命令格式:

      1)压缩文件:   zip 压缩文件名    源文件
    [root@livedvd home]# zip test.zip test.txt
    adding: test.txt (stored 0%)
    [root@livedvd home]# ll
    total 12
    drwx------. 26 centoslive centoslive 4096 Aug 12 07:40 centoslive
    -rw-r--r--. 1 root root 9 Aug 12 07:53 test.txt
    -rw-r--r--. 1 root root 175 Aug 12 07:54 test.zip

    2)压缩目录: zip -r 压缩文件名 源目录
    [root@livedvd home]# mkdir music
    [root@livedvd home]# zip -r music.zip music
    adding: music/ (stored 0%)
    [root@livedvd home]# ll
    total 20
    drwx------. 26 centoslive centoslive 4096 Aug 12 07:40 centoslive
    drwxr-xr-x. 2 root root 4096 Aug 12 07:58 music
    -rw-r--r--. 1 root root 162 Aug 12 07:58 music.zip
    -rw-r--r--. 1 root root 9 Aug 12 07:53 test.txt
    -rw-r--r--. 1 root root 175 Aug 12 07:54 test.zip

    3)解压命令: unzip 压缩文件
    [root@livedvd home]# unzip music.zip
    Archive: music.zip
    creating: music/
    [root@livedvd home]# unzip test.zip
    Archive: test.zip
    extracting: test.txt
    [root@livedvd home]# ll
    total 20
    drwx------. 26 centoslive centoslive 4096 Aug 12 07:40 centoslive
    drwxr-xr-x. 2 root root 4096 Aug 12 07:58 music
    -rw-r--r--. 1 root root 162 Aug 12 07:58 music.zip
    -rw-r--r--. 1 root root 9 Aug 12 07:53 test.txt
    -rw-r--r--. 1 root root 175 Aug 12 07:54 test.zip
  2. .gz 格式压缩

        1)gzip  源文件(压缩为.gz格式的压缩文件,源文件会消失)
    [root@livedvd home]# ls
    centoslive music test.txt
    [root@livedvd home]# gzip test.txt
    [root@livedvd home]# ls
    centoslive music test.txt.gz

    2)gzip -c 源文件 > 压缩文件(压缩为.gz格式,源文件保留)
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt.gz
    [root@livedvd home]# gzip -c test1.txt > test1.txt.gz
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.gz test.txt.gz

    3)gzip -r 目录(压缩目录下所有的子文件,但是不能压缩目录)
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.gz test.txt.gz
    [root@livedvd home]# gzip -r music
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.gz test.txt.gz
    [root@livedvd home]# cd music
    [root@livedvd music]# ls
    music1.txt.gz music2.txt.gz music3.txt.gz

    4)解压缩文件
    1> gzip -d 压缩文件
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.gz test.txt.gz
    [root@livedvd home]# gzip -d test.txt.gz
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.gz test.txt

    2> gunzip 压缩文件
    [root@livedvd home]# ls
    centoslive music test1.txt.gz test.txt
    [root@livedvd home]# gunzip test1.txt.gz
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt
  3. .bz2 格式压缩

          1)bzip2  源文件(压缩为.bz2格式,不保留源文件)
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt
    [root@livedvd home]# bzip2 test.txt
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt.bz2

    2)bzip2 -k 源文件(压缩之后保留文件)
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt.bz2
    [root@livedvd home]# bzip2 -k test1.txt
    [root@livedvd home]# ls
    centoslive music test1.txt test1.txt.bz2 test.txt.bz2

    3)bzip2 -d 压缩文件(解压缩,可以用-k保留压缩文件)
    [root@livedvd home]# ls
    centoslive music test1.txt.bz2 test.txt.bz2
    [root@livedvd home]# bzip2 -d test1.txt.bz2
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt.bz2

    4)bunzip2 压缩文件(解压缩,可以用-k保留压缩文件)
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt.bz2
    [root@livedvd home]# bunzip2 test.txt.bz2
    [root@livedvd home]# ls
    centoslive music test1.txt test.txt

强调内容
注意:bzip2命令不能压缩目录哦!

常用压缩格式:.tar.gz .tar.bz2