This article comes from "Stones" blog, http://pengyl.blog.51cto.com/5591604/1191197
Packaging, a combination of many files and directories is saved as a whole package file for easy delivery or portability. The compression operation can further reduce the amount of disk space used by the packaged package files. The most common packaging command in Linux is tar. The most commonly used compression commands are two: gzip, bzip2, compressed files made with gzip usually use the extension ". gz", compressed files made with bzip2 usually use the extension ". bz2". BZIP2 's compression efficiency is higher than gzip, but they are only for a single file compression and decompression, so it is usually first through the tar command to pack multiple files or directories into a package file, and then through gzip or bzip2 compression, such as *.tar.gz and *. TAR.BZ2 belongs to this file that is packaged and then compressed. In practice, the tar command is typically used to invoke gzip or bzip2 for compression or decompression, and rarely to use the gzip or BZIP2 commands alone.
Command: Tar feature: Used to establish, restore backup file parameters:-C or--create create a. tar format package file
-c< Purpose directory > or--directory=< Purpose directory > Specify the destination location of the extracted file (note that C is uppercase)
-f< backup File > or--file=< backup file > Specify backup file name, immediately after F to file name Oh. Don't add any more parameters. # Note that the file name after the parameter F is taken by itself, and we are accustomed to using the. Tar as an identification. If the z parameter is added, the. tar.gz or. tgz is used to represent the gzip-compressed tar file ~ If the J parameter is added, the. tar.bz2 as the file name ~
For example, the use of "TAR-ZCVFP tfile sfile" is the wrong way to write
"TAR-ZCVPF tfile sfile" is right.
-P: Use the original properties of the original file (properties will not be changed according to the user)
-P: You can use absolute paths to compress.
-Z or--gzip or--ungzip processing the backup file through the gzip instruction-J processing the backup file through the bzip2 instruction
-X or--extract or--get unlock package files in the. tar format
-T or--list lists the contents of the backup file.
-V or--verbose display instruction execution procedure
The order in which the-s or--same-order restore files is the same as in the order in which they are stored in the backup file: Package all the files in the entire/home directory into Home.tar.
[Root@localhost ~]# tar cvf home.tar/home
Root@linux ~]# tar-cvf/tmp/etc.tar/etc<== is packaged only, not compressed.
[Root@linux ~]# tar-zcvf/tmp/etc.tar.gz/etc<== after packaging, gzip compressed
[Root@linux ~]# tar-jcvf/tmp/etc.tar.bz2/etc<== after packaging to bzip2 compression
A warning message is displayed when the above instructions are executed:
# "tar:removing leading"/"from member names" is a special set of absolute paths.
Consult the files in the/tmp/etc.tar.gz file.
[Root@linux ~]# tar-ztvf/tmp/etc.tar.gz
# Since we use gzip compression, we need to check the files in the tar file
# It's going to have to add the Z parameter. It's very important.
such as abc.tar.gz, there is a file test.c, to be extracted separately to the/bak directory
Tar xvf abc.tar.gz-c/bak test.c
View the size of the generated package file [root@localhost ~]# du-h Home.tar
1012K Home.tar
Call the gzip command to package and compress all the files in the/home directory into home.tar.gz.
[Root@localhost ~]# tar zcvf home.tar.gz/home
To view the size of the generated compressed file
[Root@localhost ~]# du-h home.tar.gz
104K home.tar.gz
Call the BZIP2 command to package and compress all the files in the/home directory into home.tar.gz.
[Root@localhost ~]# tar jcvf home.tar.bz2/home
To view the size of the generated compressed file
[Root@localhost ~]# du-h home.tar.bz2
92K home.tar.bz2
Unzip the home.tar.gz to the current directory (after executing the command, create a directory named home in the current directory, with the extracted files stored)
[Root@localhost ~]# tar zxvf home.tar.gz
Extract the home.tar.bz2 into the/root/home2 directory.
[Root@localhost ~]# mkdir home2
[Root@localhost ~]# tar jxvf home.tar.bz2-c home2
See what is included in a compressed file without the pressure
[Root@localhost ~]# tar ztvf home.tar.gz | More
[Root@localhost ~]# tar jtvf home.tar.bz2 | More exclude a directory or file when using tar compression
There are 3 subdirectories in the/HOME/ABC directory: ABC1, ABC2, ABC3, ABC4, and now want to/HOME/ABC, ABC1, ABC2, Package ABC3, and do not pack abc.tar.gz.
Cd/home
TAR-ZCVF abc.tar.gz--EXCLUDE=ABC4 ABC
Zip command Detailed
Function Description: Compress file.
Syntax: Zip [-acddffghjjkllmoqrstuvvwxyz$][-b < working directory >][-ll][-n < word tail string >][-t < date time >][-< compression efficiency >][Compressed files [Document ...] [-i < template style] [-x < template style]
Supplemental Note: Zip is a widely used compression program, the file after it compression will produce a ". zip" extension of the compressed file.
Parameters
-A adjusts the executable automatic decompression file.
-b< working directory > Specifies the directory where files are temporarily stored.
-C adds a comment for each compressed file.
-d deletes the specified file from the compressed file.
The directory name is not established in the-D compressed file.
-F The effect of this parameter is similar to the specified "-u" parameter, but not only the existing file is updated, but if some files do not exist in the compressed file, this parameter will be added to the compressed file together.
-F attempts to repair a corrupted compressed file.
-G compresses the file and attaches it to the existing compressed file instead of creating a new compressed file.
-H online Help.
-i< template Style > compress only the eligible files.
-j only saves the file name and its contents, without any directory names.
-J Delete unnecessary data before compressing the file.
-K uses a file name in MS-DOS-compliant format.
-L replaces the LF character with the LF+CR character when compressing the file.
When-ll compressed files, replace the LF+CR characters with the LF character.
-L displays copyright information.
-M to compress the file and add the compressed file, delete the original file, that is, move the file to the compressed file.
-n< string > Do not compress files with a specific character end string.
-O Sets the time to change the compressed file to the same file as the file that has the latest change time in the compressed file.
-Q does not display the instruction execution process.
-R recursive processing of all files and subdirectories under the specified directory.
-S contains system and hidden files.
-t< Date Time > Set the date of the compressed file to the specified date.
-T checks that each file in the backup file is correct.
-U replaces the newer file with the compressed file.
-V Displays the instruction execution process or display version information.
-V Saves the file properties of the VMS operating system.
-W in the file name if the version number, this parameter is only valid under the VMS operating system.
-x< template style > exclude eligible files when compacting.
-X does not save additional file attributes.
-Y saves the symbolic connection directly, not the file that the connection points to, and this parameter is valid only for systems such as UNIX.
-Z Adds a comment for the compressed file.
-$ the volume name of the disk on which the first compressed file is stored.
-< compression Efficiency > Compression efficiency is a value between 1-9.
Unzip Command detailed
Function Description: Extract zip file
Syntax: Unzip [-cflptuvz][-agcjlmnoqsvx][-p < password >][.zip file] [file][-d < directory >][-x < file] or unzip [-z]
Supplemental Note: Unzip is the decompression program for the. zip compressed file.
Parameters
-C Displays the uncompressed results to the screen and converts the characters appropriately.
-F to update existing files.
-L Displays the files contained within the compressed file.
-P, similar to the-c parameter, displays the uncompressed results to the screen, but does not perform any transformations.
-T check that the compressed file is correct.
-U is similar to the-f parameter, but in addition to updating an existing file, the other files in the compressed file are uncompressed to the directory.
-V displays detailed information when it is executed.
-Z Displays only the memo text for the compressed file.
-A makes the necessary character conversions to the text file.
-B Do not convert text files to characters.
The file name in the-C compressed file is case-sensitive.
-J does not handle the directory path in the compressed file.
-L Change all file names in the compressed file to lowercase.
-M sends output to more program processing.
-N uncompressed does not overwrite the original file.
-O does not need to ask the user first, overwriting the original file after unzip execution.
-p< Password > Use zip password option.
-Q does not display any information at execution time.
-S converts the whitespace character in the file name to the bottom line character.
-V preserves file version information for VMS.
-X to save the original uid/gid of the file while decompressing.
[. zip file] Specifies a. zip compressed file.
[file] Specifies which files in the. zip compressed file to process.
-d< directory > Specifies the directory to store after the file is uncompressed.
-x< file > Specify which files in the. zip compressed file should not be processed.
-Z unzip-z equals execute zipinfo Instruction
Example:
The zip command can be used to compress files into a commonly used zip format. The unzip command is used to extract the zip file.
1. I want to compress a file abc.txt and a directory dir1 into Yasuo.zip:
# zip-r Yasuo.zip abc.txt Dir1
2. I downloaded a yasuo.zip file and wanted to decompress:
# Unzip Yasuo.zip
3. My current directory has abc1.zip,abc2.zip and abc3.zip, I would like to decompress them together:
# unzip abc\? Zip
Note:? Represents a character, if you use * to represent any number of characters.
4. I have a large compressed file Large.zip, I don't want to decompress, just want to see what's inside it:
# unzip-v Large.zip
5. I downloaded a compressed file large.zip to verify that the compressed file is completely downloaded
# unzip-t Large.zip
6. I use the-v option to find that there are many directories and subdirectories in the Music.zip compressed file, and the subdirectories are in fact songs mp3 files, I would like to download these files to the first level of the directory, rather than a layer of building a directory:
# unzip-j Music.zip
7.unzip extract to specified directory
#unzip monitor-0.8.2.zip-d Monitor
8. View Compressed Package details
[Root@localhost cactiplugins]# unzip-z Monitor.zip