Tar, 7z (7zip) compression/Decompression command, 7z7zip
This document describes how to use tar and 7z commands.
Tar command
In Linux, the most commonly used compression/Decompression command is the tar command.
The tar command is used to package the structure of multiple files/directories. In actual use, tar is often used to support compression, that is, two steps of packaging and compression are performed simultaneously. The following commonly used file formats can be generated by the tar command.
. Tar ~ Only package the file/directory structure without compressing it;
.Tar.gz ~ Package the file/directory structure and compress it in gzip format;
.Tar.bz2 ~ Package the file/directory structure and compress it in bz2 format;
. Tar. xz ~ Package the file/directory structure and compress it according to the LZMA algorithm (the Linux kernel uses this format for release );
Command Format
$ Tar -- help
Usage: tar [option...] [FILE]...
GNU 'tar 'saves program files together into asingle tape or disk archive, and can
Restore individual files from the archive.
Examples:
Tar-cf archive.tar foo bar # Createarchive.tar from files foo and bar.
Tar-tvf archive.tar # List all filesin archive.tar verbosely.
Tar-xf archive.tar # Extract allfiles from archive.tar.
The following lists the common options of the tar command:
-C: Create a compressed file
-X: Extract
-T: View content
-R: append an object to the end of the compressed archive object
-U: update files in the original compressed package
These five are independent commands, one of which must be used for compression and decompression. They can be used with other commands, but only one of them can be used. The following parameters are optional when files are compressed or decompressed as needed.
-Z: With the gzip attribute
-J: With the bz2 attribute
-J: With the xz attribute
-V: displays all processes.
-C: switch to a directory and execute the tar command.
-F: use the file name. Remember, this parameter is the last parameter and can only be followed by the file name. (This parameter must be added)
Example
$ Cd ~ /Study
$ Rm-rf *
$ Mkdir-p d1/d11
$ Mkdir d2
$ Touch d1/d11/f1.c
$ Touch d2/f2.php
$ Cd ../
# $ Tar czf study.tar.gz study /~ Pack and compress the file in the .gz format
# $ Tar cjf study.tar.bz2 study /~ Pack and compress in the. bz2 format
$ Tar cJf study.tar. xz study /~ Package and execute. xz format Compression
# $ Tar tf study.tar.gz ~ View the directory list in the compressed package
# $ Tar tf study.tar.bz2 ~ View the directory list in the compressed package
$ Tar tf study.tar. xz ~ View the directory list in the compressed package
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ Rm-rf study
# $ Tar xzf study.tar. xz ~ Decompress the. GZ compressed package to the current directory.
# $ Tar xjf study.tar. xz ~ Decompress the. bz2 compressed package to the current directory
$ Tar xJf study.tar. xz ~ Decompress the. xz format compressed package to the current directory
$ Ls
Study.tar. xz study
$ Tar xJf study.tar. xz-C /~ Decompress the package to the root directory.
$ Ls/
Bin Cygwin. bat Cygwin-Terminal.ico etc lib proc tmp var
Cygdrive Cygwin. ico dev home optStudyUsr
More information
$ Man tar
7z command
7z is a common compressed file format of the 7zip compression tool. 7zip is an open-source compression tool. The software itself is very compact and powerful. It is currently recognized as the most popular compression and decompression software in recent years.
7zip supports many compression formats, including 7z, XZ, BZIP2, GZIP, TAR, and ZIP. Supports decompression in many formats such as CAB, ISO, RAR, RPM, XAR, and Z.
Exit code 0 indicates that the program is normal and there is no error.
Command Format
$ 7z -- help
Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
[<@ Listfiles...>]
Common commands are as follows:
A. Add the file to the compressed file.
D. delete the file from the compressed file.
E. Extract the file from the compressed file, excluding the directory structure.
L list content in the compressed package
T test compressed files
U updates files to compressed files
X extract from the compressed file, including the directory structure
Example
$ Rm *-rf
$ Mkdir-p study/d1/d11 study/d2
$ Touch study/d1/d11/f1.c study/d2/f2.php
$ 7z a study.7z study /~ Copy the full studyvideo to the compressed package study.7z.
Scanning
Creating archive study.7z
Everything is OK
$ 7z t study.7z ~ Test compressed package
Processing archive: study.7z
Testing study/d1/d11/f1.c
Testing study/d2/f2.php
Testing study/d2
Testing study/d1/d11
Testing study/d1
Testing study
Everything is OK
$ Rm-rf study
$ 7z x study.7z ~ Decompress the package to the current directory.
Processing archive: study.7z
Extracting study/d1/d11/f1.c
Extracting study/d2/f2.php
Extracting study/d2
Extracting study/d1/d11
Extracting study/d1
Extracting study
Everything is OK
$ 7z d study.7z study/d2/f2.php ~ Delete a specified file in the package
Updating archive study.7z
Everything is OK
$ 7z t study.7z ~ Test compressed package
Processing archive: study.7z
Testing study/d1/d11/f1.c
Testing study/d2
Testing study/d1/d11
Testing study/d1
Testing study
Everything is OK
$ 7z l study.7z ~ View the directory structure in the compressed package
Listing archive: study.7z
--
Path = study.7z
Type = 7z
Solid =-
Blocks = 0
Physical Size = 152
Headers Size = 152
Study/d1/d11/f1.c
Study/d2
Study/d1/d11
Study/d1
Study
$ 7z u study.7z study/d1/d11/f1.c ~ Update a specified file in the compressed package
Scanning
Updating archive study.7z
Compressing study/d1/d11/f1.c
Everything is OK
$ 7z l study.7z ~ View the directory structure in the compressed package
Listing archive: study.7z
--
Path = study.7z
Type = 7z
Method = LZMA
Solid =-
Blocks = 1
Physical Size = 260
Headers Size = 186
Study/d1/d11/f1.c
Study/d2
Study/d1/d11
Study/d1
Study
7zip compression format Control
The above example demonstrates the use of the 7z compression/Decompression command. When using 7z for compression/decompression, the compression format is identified based on the file Suffix of the compressed package provided in the command. For example, 7z a study.zip study/compresses the directory tree study in zip format, and 7z a study.7z study/compresses the directory tree study in 7z format. Otherwise, the compression format is automatically identified based on the file name suffix of the compressed package.
You can refer to the following implementation for compatibility between different types of archives (tar.gz tar.bz2 tar. xz) of '7zip' and 'tar.
$ 7z a study4.tar study ~ Step 1 first package in tar format
# According to the experiment results, this command has the same effect as tar cf study4.tar study.
Scanning
Creating archive study4.tar
Compressing study
Compressing study/d1
Compressing study/d1/d11
Compressing study/d1/d11/f1.c
Compressing study/d2
Compressing study/d2/f2.php
Everything is OK
$ 7z a study4.tar.gz study4.tar ~ Compress the packaged files in gzip format
Scanning
Creating archive study4.tar.gz
Compressing study4.tar
Everything is OK
$ Tar tf study4.tar ~ Use the tar command to test the file package. No error occurs.
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ Tar tf study4.tar.gz ~ Use the tar command to test the compressed package. No error occurs.
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ Rm study-rf
$ Tar xzvf study4.tar.gz ~ Uncompress the .tar.gz package generated by 7zusing tar.gz. The package is successfully decompressed.
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ Rm *.*
$ Ls
Study
$ Tar czf study4.tar.gz study ~ Generate a. tar.gz compressed package using the tarinstruction
$ 7z x study4.tar.gz ~ Decompress the compressed package generated by tar using 7z.
Processing archive: study4.tar.gz
Extracting study4.tar
Everything is OK
$ 7z x study4.tar ~ Use 7z for further decompression, succeeded
Processing archive: study4.tar
Extracting study
Extracting study/d1
Extracting study/d1/d11
Extracting study/d1/d11/f1.c
Extracting study/d2
Extracting study/d2/f2.php
Everything is OK
$
$ Tar cvf study5.tar study ~ Use tar to create a file package without compression
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ 7z a study5.tar.bz2 study5.tar ~ Use 7zip to compress the file package
Scanning
Creating archive study5.tar.bz2
Compressing study5.tar
Everything is OK
$ Tar tf study5.tar.bz2 ~ Use tar to test the compressed package
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$ 7z x study5.tar.bz2 ~ Decompress the package using 7zip
Processing archive: study5.tar.bz2
File study5.tar
Extracting study5.tar
Everything is OK
$ Rm-rf study
$ 7zx study5.tar ~ Use 7zip to restore the file package generated by the tar command
Processing archive: study5.tar
Extracting study
Extracting study/d1
Extracting study/d1/d11
Extracting study/d1/d11/f1.c
Extracting study/d2
Extracting study/d2/f2.php
Everything is OK
$ Rm study-rf
$ Tar xjvf study5.tar.bz2 ~ Uncompress the .tar.bz2 compressed package generated by using tarrent7zip.
Study/
Study/d1/
Study/d1/d11/
Study/d1/d11/f1.c
Study/d2/
Study/d2/f2.php
$
How to Use 7zip to batch compress files
You can use 7zip to batch compress files. The command line is as follows:
7z.exe a-t7z FileName.7z drive letter: \ path \ *-r
Note:
The 1st command must be executed in the 7zinstallation directory, that is, the 7z.exe directory. The compressed file library after the command is successfully run will also be stored in the directory where 7z.exe is located.
The file name behind 22.16name.7zcan also be compressed into. ZIP format. Change the parameter to-tzip FileName.zip.
3. The path is the path of the folder to which you want to compress. If the path contains spaces, enclose them with quotation marks and change it to the format of "drive letter: \ path.
How to decompress 7zip
7-zip .7z format
7-Zip is an open-source compression software with a very high compression ratio.
Supported formats:
Compression/Decompression: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM
Extract only: ARJ, CAB, CHM, CPIO, CramFS, DEB, DMG, FAT, HFS, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, RAR, RPM, squashFS, UDF, VHD, WIM, XAR, Z
For ZIP and GZIP formats, 7-Zip provides 2-10% higher compression ratio than PKZip and WinZip.
Chinese version: sparanoid.com/lab/7z/
After installation, right-click the system and the 7-Zip menu will appear. The usage is similar to that of rar.
Reference: sparanoid.com/lab/7z/