File packaging and compression
Self feeling: it's easy to learn how to pack and compress files. Although there are many compression names, several are not commonly used, and several commonly used options are basically universal. It will produce a good result. Just learn the option of one command and remember the names of the remaining commands.
This article focuses on the tar command. Most of the files on the Internet are packed and compressed.
Common compressed file extensions in linux: *. GZ *. Bz2 *. XZ *. tar *. tar.gz *. tar.bz2 *. tar.xz *. tgz *. Z *. tar.Z
Example:
1 #Demonstrate searching for keywords without decompressing 2 [root@CentOS8 tmp]# zgrep http services.gz 3 # http://www.iana.org/assignments/port-numbers 4 http 80/tcp www www-http # WorldWideWeb HTTP 5 http 80/udp www www-http # HyperText Transfer Protocol 6 http 80/sctp # HyperText Transfer Protocol 7 https 443/tcp # http protocol over TLS/SSL 8 https 443/udp # http protocol over TLS/SSL 9 https 443/sctp # http protocol over TLS/SSL 10 gss-http 488/tcp 11 gss-http 488/udp 12 13 14 #-c Options are used in combination with pipe characters, mainly for saving source files 15 [root@CentOS8 tmp]# gzip -c services > services.gz 16 [root@CentOS8 tmp]# ll 17 total 824 18 -rwx------. 1 root root 1379 May 2 08:53 ks-script-drjaqc7p 19 -rw-r--r-- 1 root root 692241 May 3 17:35 services 20 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz
Example:
1 #Keep the source file compressed and display the compression ratio information after compression 2 [root@CentOS8 tmp]# bzip2 -kv services 3 services: 5.334:1, 1.500 bits/byte, 81.25% saved, 692241 in, 129788 out. 4 [root@CentOS8 tmp]# ll 5 total 952 6 -rwx------. 1 root root 1379 May 2 08:53 ks-script-drjaqc7p 7 -rw-r--r-- 1 root root 692241 May 3 18:15 services 8 -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 9 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz 10 11 [root@CentOS8 tmp]# bunzip2 services.bz2
Example:
#Keep source file compression, and output compression ratio and other information [root@CentOS8 tmp]# xz -kv services services (1/1) 100 % 103.4 KiB / 676.0 KiB = 0.153 [root@CentOS8 tmp]# ll total 1056 -rw-r--r-- 1 root root 692241 May 3 18:15 services -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz -rw-r--r-- 1 root root 105872 May 3 18:15 services.xz [root@CentOS8 tmp]# rm services #Delete source file rm: remove regular file 'services'? y [root@CentOS8 tmp]# xz -dk services.xz #Keep the source file uncompressed [root@CentOS8 tmp]# ll total 1056 -rw-r--r-- 1 root root 692241 May 3 18:15 services -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz -rw-r--r-- 1 root root 105872 May 3 18:15 services.xz #Search for compressed packages using xzgrep [root@CentOS8 tmp]# xzgrep http services.xz # http://www.iana.org/assignments/port-numbers http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL https 443/udp # http protocol over TLS/SSL https 443/sctp # http protocol over TLS/SSL #Use-l Option to display long format information [root@CentOS8 tmp]# xz -l services.xz Strms Blocks Compressed Uncompressed Ratio Check Filename 1 1 103.4 KiB 676.0 KiB 0.153 CRC64 services.xz
Example:
1 #Use-z compress 2 [root@CentOS8 tmp]# tar -zcvf etc.gz /etc/* 3 tar: Removing leading `/' from member names 4 /etc/adjtime 5 tar: Removing leading `/' from hard link targets 6 /etc/aliases 7 /etc/alternatives/ 8 ... 9 #Use - j compression and keep source file properties 10 [root@CentOS8 tmp]# tar -jcvpf etc.bz2 /etc/* 11 tar: Removing leading `/' from member names 12 /etc/adjtime 13 tar: Removing leading `/' from hard link targets 14 /etc/aliases 15 /etc/alternatives/ 16 /etc/alternatives/libnssckbi.so.x86_64 17 ... 18 #Use - J compression, and keep the source file attributes and facl attributes. The - v option is not used here 19 [root@CentOS8 tmp]# tar --xattrs -Jcpf etc.xz /etc/* 20 tar: Removing leading `/' from member names 21 tar: Removing leading `/' from hard link targets 22 [root@CentOS8 tmp]# ll 23 total 11508 24 -rw-r--r-- 1 root root 3654689 May 3 19:05 etc.bz2 25 -rw-r--r-- 1 root root 5053462 May 3 19:03 etc.gz 26 -rw-r--r-- 1 root root 3069780 May 3 19:07 etc.xz 27 28 #Use the - C option to specify the extraction directory 29 [root@CentOS8 tmp]# mkdir test 30 [root@CentOS8 tmp]# tar xf etc.gz -C /tmp/test
1 #The example shows that cpio does not distinguish between absolute path and relative path 2 [root@CentOS8 /]# find ./etc | cpio -H newc -o > /tmp/etc.cpio 3 40756 blocks 4 [root@CentOS8 /]# find /etc | cpio -H newc -o > /tmp/etc2.cpio 5 40757 blocks 6 [root@CentOS8 /]# cd /tmp 7 [root@CentOS8 tmp]# ls 8 boot.cpio etc2.cpio etc.cpio 9 [root@CentOS8 tmp]# cpio -tv < etc.cpio 10 drwxr-xr-x 78 root root 0 May 3 18:14 etc 11 lrwxrwxrwx 1 root root 19 May 2 08:51 etc/mtab -> ../proc/self/mounts 12 -rw-r--r-- 1 root root 615 May 2 08:50 etc/fstab 13 -rw------- 1 root root 0 May 2 08:50 etc/crypttab 14 -rw-r--r-- 1 root root 69 May 3 17:31 etc/resolv.conf 15 . . . 16 [root@CentOS8 tmp]# cpio -tv < etc2.cpio 17 drwxr-xr-x 78 root root 0 May 3 18:14 /etc 18 lrwxrwxrwx 1 root root 19 May 2 08:51 /etc/mtab -> ../proc/self/mounts 19 -rw-r--r-- 1 root root 615 May 2 08:50 /etc/fstab 20 -rw------- 1 root root 0 May 2 08:50 /etc/crypttab 21 -rw-r--r-- 1 root root 69 May 3 17:31 /etc/resolv.conf 22 drwxr-xr-x 8 root root 0 May 2 08:51 /etc/dnf 23 #Comparison found, using find /etc generate cpio The root directory should be kept in the file. If you unzip it, you will overwrite the files in the corresponding root directory. It's very dangerous 24 #And use find ./etc Generated cpio The root directory is not reserved in the file. 25 26 #Unzip etc.cpio 27 [root@CentOS8 tmp]# ls 28 boot.cpio etc2.cpio etc.cpio 29 [root@CentOS8 tmp]# mkdir test 30 [root@CentOS8 tmp]# cd test 31 [root@CentOS8 test]# cpio -id <../etc.cpio 32 40756 blocks 33 [root@CentOS8 test]# ls 34 etc
1 [root@CentOS8 tmp]# rm -fr * 2 [root@CentOS8 tmp]# ls 3 [root@CentOS8 tmp]# cp /etc/services . 4 [root@CentOS8 tmp]# ls 5 services 6 [root@CentOS8 tmp]# time gzip -k services ;time bzip2 -k services ;time xz -k services 7 8 real 0m0.016s #Use gzip Time consumed by compression0.016s 9 user 0m0.013s 10 sys 0m0.003s 11 12 real 0m0.036s #Use bzip2 Time consumed by compression0.036s 13 user 0m0.025s 14 sys 0m0.010s 15 16 real 0m0.184s #Use xz Time consumed by compression0.184s 17 user 0m0.107s 18 sys 0m0.077s 19 [root@CentOS8 tmp]# ll 20 total 1052 21 -rw-r--r-- 1 root root 692241 May 3 18:36 services 22 -rw-r--r-- 1 root root 129788 May 3 18:36 services.bz2 23 -rw-r--r-- 1 root root 142549 May 3 18:36 services.gz 24 -rw-r--r-- 1 root root 105872 May 3 18:36 services.xz
To summarize:
Common compressed file extensions in linux: *. GZ *. Bz2 *. XZ *. tar *. tar.gz *. tar.bz2 *. tar.xz *. tgz *. Z *. tar.Z
The common compression commands are: gzip bzip2 xz. The best compression ratio is xz, but the time consumed is also the highest. Time cost and CPU performance need to be considered.
Several options are basically common: - d decompress - v display detailed process - c output content to screen - k keep source file
tar can package files and directories, and gzip bzip2 xz can be called for compression when packaging
Compression: tar -[zjJ]cv -f filename. Suffix file or directory to be compressed
Query: tar - [zjJ]tv -f filename. Suffix
Unzip: tar - [zjJ]xv -f filename. Suffix - C / path / to / media
The cpio command needs to be used with the find command when compressing. Pay attention to using relative path