Use the tar command to archive and compress files


Archive and compress files

Benefits of archiving and compressing files: save hard disk resources and speed up file transfer rate
The tar command is used to package and compress files
Use the tar command to archive and compress files
In this figure, 123456 files are packaged into a.tar file, but the a.tar file is still large. Continue to use it
Gzip is compressed into a smaller compressed file.

Function: package and compress files; Tar file is a collection of several files and / or directories in one file. The archive file can be
Use gzip, bzip2, or XZ compression tools to compress rows and then transfer them

Look at man tar here man doesn’t mean man, it’s the abbreviation of manual manual
Usage: Tar [option…] [file]

-C create create file
-x  -extract [ ˈ Ekstr(kt] extract, decompress and restore files
-V -- verbose displays the detailed execution process
-F -- file specifies the backup file
-T -- list which files are included in the compressed package. Check the contents of the package without unpacking
-C (uppercase) -- directory

Specify the decompression location

Example 1: package the / boot / Grub2 directory

[ [email protected] ~]#'-' may not be used before the parameters of tar - CVF grub.tar / boot / Grub2 / # tar
[[email protected] ~]#   tar cvf grub.tar /boot/grub2/
[[email protected] ~]# tar cf grub.tar /boot/grub2/

Tar: delete the beginning “/” from the member name (that is, delete the / root path, which becomes the boot / Grub2 relative path. Solution
When pressing, it will be decompressed to the current directory. If it is not deleted, it will be / boot / Grub2. When decompressing, it will be the absolute path and will be overwritten
Files in the system at this path)

[[email protected] ~]# tar -cf grub.tar /boot/grub2/

Tar: remove the beginning ‘/’ from the member name

[[email protected] ~]# ls grub.tar
[ [email protected] ~]#Tar xvf grub.tar # unzip
[ [email protected] ~]#Ls boot # gets the boot directory

Example 2: package two directories or directories + files into an archive package:

[email protected] ~]# mkdir ./back
[[email protected] ~]# cp /etc/passwd ./back/
[[email protected] ~]# tar -cvf back.tar /boot/grub   /root/back/ /etc/passwd
Tar: remove the beginning '/' from the member name
-Rw-r -- R -- root / root 1024 2020-06-28 19:46 boot / Grub2 / grubenv ### output content
-rw-r--r-- root/root  5130 2020-06-28 19:46 boot/grub2/grub.cfg
drwxr-xr-x root/root   0 2020-07-03 14:23 root/back/
-rw-r--r-- root/root    2735 2020-07-03 14:23 root/back/passwd
-rw-r--r-- root/root    2735 2020-06-28 19:49 etc/passwd

Example 3: check the contents in tar without unpacking:

[[email protected] ~]# tar -tvf back.tar             # List all files in archive.tar verbosely.

Example 4: compare the effect of adding v

[email protected] ~]# tar -xf back.tar
[[email protected] ~]# tar -xvf back.tar

Tar archiving + compression

Syntax: tar czvf newfile.tar.gz source
Syntax: tar czvf compressed file name (tar. GZ tar. Bz2) the file or directory to be compressed
Common parameters:

-z. -- gzip compress the extension as gzip: tar.gz
-J: extension compressed in bz2 mode: tar.bz2
-J: Compress the extension as XZ: tar.xz

Example 1: create the. Tar.gz package

[[email protected] ~]# tar cvf /root/etc.tar /etc
[ [email protected] Test]# tar zcvf / root / etc.tar.gz / etc # archive. Pay attention to the suffix of the backup name
[ [email protected] Test]# tar zxvf / root / etc.tar.gz # unzip
[ [email protected] Test]# tar xvf / root / etc.tar.gz # unzip

Example 2: create the. Tar.bz2 package

Syntax: #tar jcvf newfile.tar.bz2 source
[[email protected] ~]#   tar -jcvf ~/etc.tar.bz2 /etc
[ [email protected] ~]#Tar -jxvf ~ / etc.tar.bz2 # decompression
[ [email protected] ~]#Tar - xvf ~ / etc.tar.bz2 # decompression
[ [email protected] ~]#Tar jxvf ~ / etc.tar.bz2 - C / opt # unzip to the opt directory

Example 3: create the. Tar.xz package

[[email protected] ~]#   tar -Jcvf ~/etc.tar.xz /etc
[ [email protected] ~]#Tar - jxvf ~ / etc.tar.xz #tar.xz package, unzip
[[email protected] ~]#   tar -xvf ~/etc.tar.xz

Compare the compression ratio after three compression methods:

[[email protected] ~]# time tar zcf /root/etc.tar.gz /etc
[[email protected] ~]# time tar jcf /root/etc.tar.bz2 /etc
[[email protected] ~]# time tar Jcf /root/etc.tar.xz /etc
[ [email protected] ~]#Ll - H etc.tar * (* is a wildcard, representing any character any time)
-Rw-r -- R -- 1 0 root 28m May 10 12:10 etc.tar
-Rw-r -- R -- 1 0 root 8.7m May 10 12:14 etc.tar.bz2# common
-Rw-r -- R -- 10 root 9.8m May 10 12:11 etc.tar.gz # common
-Rw-r -- R -- 10 root 7.0m at 12:16 on May 10, etc.tar.xz # has the highest compression ratio and the longest compression time

View source file size

[[email protected] ~]# du -sh /etc
31M /etc

The etc.tar package is 28m, which is hardly compressed. XZ is formatted as 7.0m, which greatly improves the efficiency during transmission.

Let’s share it for you today. There are surprises below.
Use the tar command to archive and compress files

If you want to get this book for free, add me to send a message: k8s books can get this book for free.

Use the tar command to archive and compress files