Compression and packing (part two)
In addition to gzip, bzip2, and xz, linux also has a compression program. It supports compressed directories and can also extract files of the same type in windows. It is the zip we know.
1、zip
Command to zip 1.txt.zip 1.txt zip -r 123.zip 123/zip directory unzip 1.txt.zip unzip 123.zip-d /root/456/unzip to specified directory unzip -l 123.zip List the contents of the compressed file
Test Example:
[root@server02 ~]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 71%)[root@server02 ~]# ls -lh 1.txt*-rw-r--r--. 1 root root 21M Jun 20 17:56 1.txt-rw-r--r--. 1 root root 4.2M Jun 20 17:55 1.txt.bz2-rw-r--r--. 1 root root 5.8M Jun 20 17:35 1.txt.gz-rw-r--r--. 1 root root 21M Jun 20 17:59 1.txt.new-rw-r--r--. 1 root root 681K Jun 20 17:59 1.txt.xz-rw-r--r--. 1 root root 5.8M Jun 20 18:03 1.txt.zip[root@server02 ~]# file newdirnewdir: directory[root@server02 ~]# zip -r newdir.zip newdir adding: newdir/ (stored 0%) adding: newdir/1.txt (deflated 71%)[root@server02 ~]# unzip 1.txt.zipArchive: 1.txt.zipreplace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A inflating: 1.txt[root@server02 ~]# unzip -l newdir.zipArchive: newdir.zip Length Date Time Name--------- ---------- ----- ---- 0 06-20-2017 18:04 newdir/ 21095346 06-20-2017 18:04 newdir/1.txt--------- ------- 21095346 2 files
Unlike gzip, bzip2, and xz, the original file is retained after zip compression. You cannot change the compressed package name. You can't view the contents of the zip package directly. You can only view the list of files in the zip package.
2、tar
The tar command is a reliable way to back up files on Unix/Linux systems, works in almost any environment, and is available to all users. It is mainly used to package files.
command to package a single file with tar -cvf 123.tar 123 tar -cvf 123.tar 1.txt 123 package multiple files tar -xvf 123.tar unpackage tar -tf 123.tar view a list of packaged files tar -cvf 123.tar --exclude 1.txt --exclude 23 some files are not packaged when packaged
Test Example:
[root@server02 ~]# tar -cvf 1.tar 1.txt1.txt[root@server02 ~]# ls -lh 1.*- rw-r--r--. 1 root root 21M Jun 20 18:09 1.tar-rw-r--r--. 1 root root 21M Jun 20 17:56 1.txt[root@server02 ~]# tar -cvf 12.tar 1.txt 2.txt1.txt2.txt[root@server02 ~]# tar -tf 12.tar1.txt2.txt[root@server02 ~]# ls newdir1.txt 2.txt 3.txt [root@server02 ~]# tar -cvf 123.tar newdir --exclude 1.txtnewdir/newdir/2.txtnewdir/3.txt
The tar command can also be used to package and compress:
Command to pack and compress tar-zcvf 123.tar.gz 123 into gzip tar-zxvf 123.tar.gz unpack and decompress gzip tar-jcvf 123.bz2 123 into bzip tar-jxvf 123.bz2 unpack and decompress bzip tar-Jcvf 123.xz 123 into xz tar-Jxvf 123.xz unpack and decompress xz tar-tf 123.bz2 123.gz/123.xz View archive file list
Test Example:
[root@server02 ~]# tar -zcvf 1.txt.tar.gz 1.txt1.txt[root@server02 ~]# ls -lh 1.txt.tar.gz-rw-r--r--. 1 root root 5.8M Jun 20 18:15 1.txt.tar.gz[root@server02 ~]# tar -zxvf 1.txt.tar.gz1.txt[root@server02 ~]# tar -jcvf 1.txt.bz2 1.txt1.txt[root@server02 ~]# ls -lh 1.txt.bz2-rw-r--r--. 1 root root 4.2M Jun 20 18:18 1.txt.bz2[root@server02 ~]# tar -jxvf 1.txt.bz21.txt[root@server02 ~]# tar -Jcvf 1.txt.xz 1.txt1.txt[root@server02 ~]# ls -lh 1.txt.xz-rw-r--r--. 1 root root 680K Jun 20 18:22 1.txt.xz[root@server02 ~]# tar -Jxvf 1.txt.xz1.txt