Archive and compress commands of Linux files


1. File archiving command tar

Benefits of archiving:

 easy to use, query and read

 easy to manage (bulk delete files)

Why compress

If the direct transmission will occupy a large amount of traffic bandwidth, resulting in the company’s intranet access speed is slow

Compress before transmission –  decompress after transmission

Benefits of file compression

Save hard disk resources

Speed up file transfer

Command used: Tar

Functions: package and compress files; tar file is a collection of several files and / or directories in one folder. Is the best tool for creating backups and Archives

[[email protected] ~]# tar –help 
[root @ xuegod72 ~] ා tar - CF archive.tar foo bar ා package foo bar file into. Tar 
[root @ xuegod72 ~] ා tar - tvf archive.tar ා list all files in tar 
[root @ xuegod72 ~] ා tar - XF archive.tar ා advance or release files in tar


Syntax: name of tar package target file / directory


 – C create create file
 – x decompress the restore file
 – V show detailed process of execution
 – f designated backup file
 – t list backup file contents
 – P (upper case) does not remove the root before the file name when using absolute paths,
 – C (upper case) specifies the decompression location
 – Z compression by gzip
 – J decompress through bzip2

Note: when starting the package name, the suffix should be added according to the type you want to compress

[root @ xuegod72 MNT] ා tar – CVF grub2.tar / boot / Grub2 / – Archive
[root @ xuegod72 MNT] ා tar – tvf grub2.tar| more – view files in the archive
[root @ xuegod72 MNT] ා tar – cvfp grub2.tar / boot / Grub2 / – archive root (dangerous, may overwrite root after decompression)
[root @ xuegod72 MNT] ා tar – xvf grub2.tar – C / usr / SRC / – extract release – C specified location

In Linux, how do you distinguish file types and suffixes

For example, SH script AA

File command, viewing file types

Linux is not as strict with file extension as windows requires, so in the process of using Linux, some files often have no extension at all, so how can we judge whether files without extension are files or directories?

Role: determine file type

Syntax: File filename

[[email protected] mnt]# file grub2.tar 
[[email protected] mnt]# file /etc/init.d/nginx

File file type

file size

Size of comparison file:

[[email protected] mnt]# du -sh /boot/grub2/ 
[[email protected] mnt]# ll -h /boot/grub2/

2. Linux file compression

Compression tool:gzip bzip2 zip tar

3.1 compression format

Common compression formats: GZ, bz2, XZ, zip, Z

Format (file name format):. Tar.gz or. Tgz

Syntax:tar zcvf newfile.tar.gz SOURCE

Tar.gz format

[root @ xuegod72 MNT] ා tar ZCF grub2.tar.gz / boot / Grub2 ා package compression 
[root @ xuegod72 MNT] ා tar zxf grub2.tar.gz ා unzip

Tar.bz2 format

[root @ xuegod72 MNT] ා tar JCF grub2.tar.gz / boot / Grub2 ා package compression 
[root @ xuegod72 MNT] ා tar JXF grub2.tar.gz ා unzip

Rar format

[root @ xuegod72 MNT] ා rar a grub2.rar / boot / Grub2 ා package compression 
[root @ xuegod72 MNT] ා rar x grub2.tar.gz ා unzip

ZIP format

[root @ xuegod72 MNT] ා zip - R / boot / Grub2 ා pack and compress 
[root @ xuegod72 MNT] ා unzip grub2.tar.gz - D / tmp ා unzip

Gzip format

[root @ xuegod72 MNT] ා gzip grub2.tar ා pack and compress 
[root @ xuegod72 MNT] ා gzip - D grub2.tar.gz ා unzip

Bzip2 format

[root @ xuegod72 MNT] ා bzip2 - K grub2.tar ා pack and compress 
[root @ xuegod72 MNT] ා bzip2 - D grub2.tar.bz2 ා unzip

XZ format

[root @ xuegod72 MNT] ᦇ XZ - ZK grub2.tar ᦇ package compression 
[root @ xuegod72 MNT] ා unxz - DK grub2.tar.xz ා


The above is the Linux file archiving and compression command that Xiaobian introduced to you. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply you in time. Thank you very much for your support of the developepaer website!