1.tar
Tar command format [-zjxcvfpP] filename tar
- z: Represents simultaneous gzip compression.
- j: Represents the simultaneous compression with bzip2.
- J: Represents the simultaneous compression with xz.
- x: Represents unpacking or decompression.
- t: To view the files in the tar package.
- c: Represents the creation of a tar package or a compressed file package.
- v: Representation visualization.
- f: Following the filename (that is - f filename), the compressed file is called filename, or the decompressed file is filename. In the case of multiple parameter combinations, please write the - f parameter to the end.)
- P: Represents properties that use the original file. (Not often used)
- p: Indicates that absolute paths can be used. (Not often used)
exclude filename: This means that when packaging or compressing, do not include filename files. (Not often used)
[root@localhost ~]# cd test -bash: cd: test: No file or directory [root@localhost ~]# cd /tmp/8/test [root@localhost test]# xz -d 1.txt.xz xz: 1.txt.xz: No file or directory [root@localhost test]# ls 1.txt [root@localhost test]# xz -z 1.txt.xz xz: 1.txt.xz: No file or directory [root@localhost test]# xz -z 1.txt [root@localhost test]# ls 1.txt.xz [root@localhost test]# xz -d 1.txt.xz [root@localhost test]# mkdir test111 [root@localhost test]# touch test111/2.txt [root@localhost test]# echo "nihao" >!$ echo "nihao" >test111/2.txt [root@localhost test]# echo "nihao" >test111/2.txt [root@localhost test]# cp 1.txt test111/
[root@localhost test]# tree . . ├── 1.txt └── test111 ├── 1.txt └── 2.txt 1 directory, 3 files [root@localhost test]# tar -cvf test111.tar test111 test111/ test111/2.txt test111/1.txt [root@localhost test]# ls 1.txt test111 test111.tar
tar packaged file
[root@localhost test]# rm -f test111.tar [root@localhost test]# tar -cf test.tar test111 1.txt [root@localhost test]# ls 1.txt test111 test.tar
Packing and unpacking the original file will not be deleted
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt test.tar [root@localhost test]# tar -xvf test.tar test111/ test111/2.txt test111/1.txt 1.txt
- exclude option: You can exclude files or delete files.
[root@localhost test]# tar -cvf test111.tar --exclude 1.txt test111 test111/ test111/2.txt
[root@localhost test]# mkdir test111/test222 [root@localhost test]# tar -cvf test111.tar --exclude test222 test111 test111/ test111/2.txt test111/1.txt
zip command
zip commands compress directories and files, compress directories, need to specify the directory files.
[root@localhost test]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 64%) [root@localhost test]# zip test111.zip test111/* adding: test111/1.txt (deflated 64%) adding: test111/2.txt (stored 0%) adding: test111/test222/ (stored 0%)
Zip is followed by the target file name, the compressed custom compressed package name, and then by the file or directory to be compressed. Install zip directory command: Yum install-y zip
Zip-r: Compress files in the secondary directory together.
[root@localhost test]# zip -r test111.zip test111/ updating: test111/1.txt (deflated 64%) updating: test111/2.txt (stored 0%) updating: test111/test222/ (stored 0%) adding: test111/ (stored 0%)
Unzip: Unzip command. Install the unzip command: Yum install-y unzip.
[root@localhost test]# unzip 1.txt.zip Archive: 1.txt.zip replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: 1.txt [root@localhost test]# yum install -y unzip //Loaded plug-ins: fastest mirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.163.com * updates: mirrors.163.com //The package unzip-6.0-19.el7.x86_64 is installed and is the latest version //No treatment is required.
Packing and Compression
The tar command supports gzip compression, bzip2 compression, and xz compression.
Compressed into gzip format file
[root@localhost test]# tar -czvf test111.tar.gz test111 test111/ test111/2.txt test111/1.txt test111/test222/
With the - tf option, you can view a list of files for packages or compressed packages.
[root@localhost test]# tar -tf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# tar -tf test.tar test111/ test111/2.txt test111/1.txt 1.txt
Using the - zxvf option, you can decompress the tar.gz format compression package.
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt 1.txt.zip test111.tar test111.tar.gz test111.zip test.tar [root@localhost test]# tar -zxvf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# ls 1.txt 1.txt.zip test111 test111.tar test111.tar.gz test111.zip test.tar
Using bzip2 compression while packaging
Use the - cjvf option to compress:
[root@localhost test]# tar -cjvf test111.tar.bz2 test111 test111/ test111/2.txt test111/1.txt test111/test222/
Use the - tf option to view the list of compressed packages:
[root@localhost test]# tar -tf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/
Use - jxvf t:
[root@localhost test]# tar -jxvf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/