Archive and compress files

Benefits of archiving and compressing files: save resources on hard disk and speed up file transfer rate

Tar is used to package and compress files

The 123456 files are packaged into a single a.tar file. However, the a.tar file is still large. Keep using it

Gzip compresses it into a smaller compressed file.

Function: package and compress files; A tar file is a collection of several files and/or directories in a single file that can be transmitted by line compression using a compression tool such as gzip, bzip2, or xz

3. Check out man tar, which is an abbreviation of manual manual: tar [OPTION…] [FILE]… Parameters:

-c create file -x-extract [KOMP] -v --verbose show the procedure -f --file specify backup files -t --list which files are included in the compressed file. Look at the contents of the package -C (uppercase) --directory

Specify the decompression location

Example 1: Package the /boot/grub2 directory

[root@xuegod63 ~]# tar-cvf grub. Tar /boot/grub2/ # tar can be used without using the '-' [ring ~]# tar CVF grub. Tar /boot/grub2/ [root@xuegod63 ~]# tar cf grub.tar /boot/grub2/

Tar: Remove the opening “/” from the member name (the/root path becomes the relative path of boot/grub2. If uncompressed, the path becomes /boot/grub2. When uncompressed, it becomes the absolute path and overwrites the files in this path).

[root@xuegod63 ~]# tar -cf grub.tar /boot/grub2/

Tar: Removes the leading “/” from the member name

[root@xuegod63 ~]# ls grub. Tar # boot/grub2/ boot/grub2/ [directory] ~]# ls boot # get the boot directory grub2

Example 2: Pack two directories or directories + files into one archive package:

root@xuegod63 ~]# mkdir ./back [root@xuegod63 ~]# cp /etc/passwd ./back/ [root@xuegod63 ~]# tar -cvf back.tar /boot/grub  /root/back/ /etc/passwd tar: -rw-r--r-- root/root 1024 2020-06-28 19:46 boot/grub2/grubenv ### -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 of tar without knowing the package:

[root@xuegod63 ~]# tar -tvf back.tar             # List all files in archive.tar verbosely.

Example 4: Contrast the effect of adding V

root@xuegod63 ~]# tar -xf back.tar
[root@xuegod63 ~]# tar -xvf back.tar

Tar archive + compression

Tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.gz SOURCE: tar CZVF newfile.tar.bz2

-z, --gzip compression extension: tar.gz -j: bz2 compression extension: tar. bz2-j: xz compression extension: tar.xz

Example 1: Create the.tar.gz package

[root@xuegod63 ~]# tar CVF /root/etc.tar /etc [PHP]# tar ZCVF /root/etc.tar.gz /etc # archive [root@localhost test]# tar ZXVF /root/etc.tar.gz # tar XVF /root/etc.tar.gz # tar XVF /root/etc.tar.gz # tar XVF /root/etc.tar.gz # tar XVF /root/etc.tar.gz # tar

Example 2: Create the.tar.bz2 package

Grammar:  #tar jcvf newfile.tar.bz2 SOURCE [root@xuegod63 ~]# tar -jcvf ~/etc.tar.bz2 /etc [root@xuegod63 ~]# tar -jxvf # tar JXVF ~/ etc.tar.bz2-c /opt # tar JXVF ~/ etc.tar.bz2-c /opt # tar JXVF ~/ etc.tar.bz2-c Opt directory

Example 3: Create the.tar.xz package

[root@xuegod63 ~]# tar-jcvf ~/etc.tar.xz /etc # tar-jxvf ~/etc.tar.xz #tar.xz /etc Unzip [root@xuegod63 ~]# tar-xvf ~/etc.tar.xz

Comparison of three compression methods after compression ratio:

[root@xuegod63 ~]# time tar zcf /root/etc.tar.gz /etc [root@xuegod63 ~]# time tar jcf /root/etc.tar.bz2 /etc [root@xuegod63 ~]# time tar Jcf /root/etc.tar.xz /etc []# ll-h etc.tar* -rw-r--r-- 10 root 28M May 10 12:10 etc.tar.bz2# -rw-r--r-- 10 root 8.7m May 10 12:14 etc.tar.bz2# -rw-r--r-- 10 root 8.7m May 10 12:14 etc.tar.bz2# 10 root 9.8m May 10 12:11 etc.tar.gz # -rw-r--r-- 10 root 7.8m May 10 12:16 etc.tar.xz #

View the source file size

[root@xuegod63 ~]# du -sh /etc
31M /etc

The etc.tar package is 28M, which is barely compressed. Xz is formatted to 7.0M, which is much more efficient when transmitting.

Today for everyone to share this, there are surprises below oh.

If you want to get this book for free, you can get this book for free by sending me the information: K8S books.