Linux compression and decompression

Compression and decompression

  • Earliest: compress / uncompress filename suffix:. Z

  • Then: gzip/gunzip filename suffix:. gz

    For a long time, gzip was the standard compression and decompression algorithm of linux

  • Then: bzip2 / bunizp2 filename suffix:. bz2

    The compression ratio of large files is a little larger than that of gzip, but the compression ratio of small files is not as large as that of gzip, so it fails to shake the position of gzip

  • Then: xz/unxz filename suffix:. xz

    Significantly improved compression ratio, shaking the position of gzip

    lzma/unlama filename suffix:. lzma

  • The most common (so supported by the operating system): zip/unzip

Linux Archive: the above compression tools can only compress files, not directories. To compress the directory, you must archive it before compressing the archive file.

  • tar´╝îcpio

gzip/gunzip/zcat

1. Compress: gzip file

After compression, the original file will be deleted automatically

# ll -h messages
-rw-------. 1 root root 915K Feb 11 22:05 messages
[root@localhost ~]# gzip messages
[root@localhost ~]# ll -h messages.gz
-rw-------. 1 root root 167K Feb 11 22:05 messages.gz

2. Decompress: gzip -d or gunzip

After decompression, the compressed file will be deleted automatically

# gunzip messages.gz
[root@localhost ~]# ll -h messages
-rw-------. 1 root root 915K Feb 11 22:05 messages
# gzip -d messages.gz
[root@localhost ~]# ll -h messages*
-rw-r--r--. 1 root root 915K Feb 11 22:06 messages

3. Specify the compression ratio, which is 6 by default. The larger the compression ratio, the smaller the space of the compressed file. The range is 1-9.

# gzip -9 messages
[root@localhost ~]# ll -h messages.gz
-rw-r--r--. 1 root root 164K Feb 11 22:06 messages.gz

4. Keep the original file after compression: gzip file - C > zxx.gz

# gzip -c messages > messages.gz
[root@localhost ~]# ll -h messages*
-rw-------. 1 root root 915K Feb 11 22:05 messages
-rw-r--r--. 1 root root 167K Feb 11 22:06 messages.gz

5. Check the contents of the compressed file directly without decompressing: zcat messages.gz

bzip2/bunzip2/bzcat

1. Compress: bzip2 file

After compression, the original file will be deleted automatically

# ll
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages
# bzip2 messages
# ll
-rw-r--r--. 1 root root 69193 Feb 11 22:06 messages.bz2

2. Decompress: bzip2 -d or bunzip2

After decompression, the compressed file will be deleted automatically

# bzip2 -d messages.bz2
[root@localhost ~]# ll messages
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages
# bunzip2 messages.bz2
[root@localhost ~]# ll
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages

3. Specify the compression ratio, which is 6 by default. The larger the compression ratio, the smaller the space of the compressed file. The range is 1-9.

# bzip2 -9 messages
[root@localhost ~]# ll
-rw-r--r--. 1 root root 69193 Feb 11 22:06 messages.bz2

4. Keep the original file after compression: gzip file -k

# bzip2 -k messages
[root@localhost ~]# ll messages*
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages
-rw-r--r--. 1 root root  69193 Feb 11 22:06 messages.bz2

5. Check the contents of the compressed file directly without decompressing: bzcat messages.bz2

xz/unxz/xzcat and lzma/unlzma/lzcat

1. Compression: xz file

After compression, the original file will be deleted automatically

# xz messages
[root@localhost ~]# ll
-rw-r--r--. 1 root root 57328 Feb 11 22:06 messages.xz

2. Decompress: xz-d or unxz

After decompression, the compressed file will be deleted automatically

# xz -d messages.xz
[root@localhost ~]# ll
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages
# unxz messages.xz
# ll
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages

3. Specify the compression ratio, which is 6 by default. The larger the compression ratio, the smaller the space of the compressed file. The range is 1-9.

# xz -9 messages
[root@localhost ~]# ll
-rw-r--r--. 1 root root 57328 Feb 11 22:06 messages.xz

4. Keep the original file after compression: xz file -k

# xz -k messages
[root@localhost ~]# ll
-rw-r--r--. 1 root root 936782 Feb 11 22:06 messages
-rw-r--r--. 1 root root  57328 Feb 11 22:06 messages.xz

5. Check the contents of the compressed file directly without decompressing: xzcat messages.xz

File

1. Create an archive

-Option c: create Archive

-f option: filename of the archive

The bar can be left blank, but f must be put at the end, because the following parameter: the archive file name is for the f option

# ls
anaconda-ks.cfg  initial-setup-ks.cfg  messages  messages.xz
[root@localhost ~]# tar cf me.tar messages messages.xz
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  messages  messages.xz  me.tar

2. View the file list in the archive

-t option: view the list of files in the archive

# tar tf me.tar
messages
messages.xz

3. Archive

-x option: expand Archive

If you do not use - C, you will expand to the current directory. If you use - C, you will expand to the specified directory.

# tar xf me.tar -C /tmp
# ls /tmp/
messages
messages.xz

4. Compress archives

Archive files can be compressed with gzip, bzip2 and xz

# gzip me.tar
# ll me.tar.gz
-rw-r--r--. 1 root root 228051 Feb 12 08:53 me.tar.gz

Archive and compress

Archiving and compression can be done together

1. After archiving, compress / decompress with gzip algorithm

  • Compression: zcf

    # tar zcf me3.tar.gz messages messages.xz
    # ll me3.tar.gz
    -rw-r--r--. 1 root root 228044 Feb 12 11:43 me3.tar.gz
  • Unzip: zxf

    # tar zxf me3.tar.gz

2. After archiving, compress / decompress with bzip2 algorithm

  • Compression: jcf

    # tar jcf me3.tar.bz2 messages messages.xz
  • Decompression: jxf

    # tar jxf me3.tar.bz2

3. After archiving, compress / decompress with xz algorithm

  • Compression: Jcf

    # tar Jcf me4.tar.xz messages messages.xz
  • Unzip: Jxf

    # tar Jxf me4.tar.xz

zip/unzip: archive and compress

# zip me.zip messages messages.xz
# unzip me.zip

QQ group of mutual learning in c/c + +: 877684253

My wechat: xiaoshitou5854

Keywords: Linux Anaconda

Added by numan82 on Wed, 12 Feb 2020 10:31:23 +0200