Archive and compress files

Advantages of archiving and compressing files: Saves disk resources and speeds up file transfer rate. The tar command is used to package and compress files123456 is packaged into a single A.tar file, but the A.tar file is still very large and is compressed using gzip to make it a smaller compressed file.

Function: package, compress files; A tar file is a collection of several files and/or directories in a single file that can be transferred with row compression using a compression tool such as gzip, bzip2, or Xz

[OPTION…] [OPTION…] [OPTION…] [OPTION…] [FILE]… Parameters:

-v --verbose displays the detailed process. -f --file Specifies the backup file. -t --listList the files included in the package, unwrap the package, and look at the contents of the package -c (uppercase) --directoryCopy the code

Specify the unzip location

Example 1: Package the /boot/grub2 directory

[root@xuegod63 ~]# tar -cvf grub.tar /boot/grub2/
[root@xuegod63 ~]# tar cvf grub.tar /boot/grub2/
[root@xuegod63 ~]# tar cf grub.tar /boot/grub2/
Copy the code

/boot/grub2: /boot/grub2: /boot/grub2: /boot/grub2: /boot/grub2: /boot/grub2

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

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

[root@xuegod63 ~]# ls grub.tar
[root@xuegod63 ~]Tar XVF grub.tar
boot/grub2/
boot/grub2/device.map
[root@xuegod63 ~]Get the boot directory
grub2
Copy the code

Example 2: Package two directories or directories + files into one archive:

root@xuegod63 ~]# mkdir ./back
[root@xuegod63 ~]# cp /etc/passwd ./back/
[root@xuegod63 ~]# tar -cvf back.tar /boot/grub /root/back/ /etc/passwdTar: Remove the starting "/" -rw-r--r-- root/root from the member name1024 2020- 06- 28 19:46-rw-r--r-- root/root5130 2020- 06- 28 19:46 boot/grub2/grub.cfg
drwxr-xr-x root/root   0 202007 -- 03 14:23 root/back/
-rw-r--r-- root/root    2735 202007 -- 03 14:23 root/back/passwd
-rw-r--r-- root/root    2735 2020- 06- 28 19:49 etc/passwd
Copy the code

Example 3: Unwrap the package and view the contents of tar:

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

Example 4: Compare the effect of adding V

root@xuegod63 ~]# tar -xf back.tar
[root@xuegod63 ~]# tar -xvf back.tar
boot/grub/
boot/grub/splash.xpm.gz
Copy the code

Tar archive + compression

Gz SOURCE syntax: tar CZVF Compressed file name (tar.gz tar.bz2) Files or directories to be compressed Common parameters are as follows:

-z, --gzip Compressed in gzip mode Extension: tar.gz -j: compressed in Bz2 mode extension: tar.bz2 -j: compressed in Xz mode Extension: tar.xzCopy the code

Example 1: Creating the.tar.gz package

[root@xuegod63 ~]# tar cvf /root/etc.tar /etc
[root@localhost test]# tar ZCVF /root/ et.tar. gz /etc
[root@localhost test]Tar ZXVF /root/et.tar. gz
[root@localhost test]# tar XVF /root/etc. Tar. gz
Copy the code

Example 2: Creating the.tar.bz2 package

Grammar:#tar jcvf newfile.tar.bz2 SOURCE
[root@xuegod63 ~]# tar -jcvf ~/etc.tar.bz2 /etc
[root@xuegod63 ~]# tar -jxvf ~ /etc/tar. bz2
[root@xuegod63 ~]Tar -xvf ~ /etc/tar. bz2
[root@xuegod63 ~]# tar JXVF ~ /etc/tar. bz2 -c /opt
Copy the code

Example 3: Creating the.tar.xz package

[root@xuegod63 ~]# tar -Jcvf ~/etc.tar.xz /etc
[root@xuegod63 ~]#tar -jxvf ~ /etc/tar.xz #tar
[root@xuegod63 ~]# tar -xvf ~/etc.tar.xz
Copy the code

Compare the compression ratio of the three compression modes:

[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
[root@xuegod63 ~]# ll -h etc. Tar *
-rw-r--r-- 1 0 root  28M 510 12:10 etc.tar
-rw-r--r-- 1 0 root  8.7M 510 12:14Etc. Tar. Commonly used.bz2 # - rw - r - r -1 0 root  9.8M 510 12:11/etc/tar. gz # common -rw-r--r--1 0 root  7.0M 510 12:16/etc/tar. xz # this is the highest compression ratio and the longest compression timeCopy the code

Check the source file size

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

The etc. Tar package is 28 megabytes, which is actually barely compressed. Xz is formatted as 7.0M, which is much more efficient when transferring.

Today for everyone to share this, there are surprises.

If you want to get this book for free, you can get this book for free by sending me a message: K8S Books.