In linux, the tar, zip, and unzip commands are used to compress and decompress folders and files. The following is an example.
Syntax: gzip [Option] compressed (decompressed) file name the meaning of each option of this command is as follows:
-C writes the output to the standard output and keeps the original file. -D. decompress the compressed file. -L the following fields are displayed for each compressed file: size of the compressed file, size of the uncompressed file, and compression ratio; name of the uncompressed file-r recursively searches for the specified directory and compresses all the files or decompress them. -T test to check whether the compressed file is complete. -V displays the file name and compression ratio for each compressed and decompressed file. -Num uses the specified numeric num to adjust the compression speed.-1 or -- fast indicates the fastest compression method (low compression ratio), and-9 or -- best indicates the slowest compression method (high compression ratio ). The default value is 6. Command instance:
Gzip * % compresses each file in the current directory into A. gz file. Gzip-dv * % decompress each compressed file in the current directory and list detailed information. Gzip-l * % detailed information of each compressed file in Example 1 is displayed without decompression. Gzip usr.tar %: the compressed tar backup file usr.tar. the extension name of the compressed file is .tar.gz.
1. tar
Usage example
| The Code is as follows: |
Copy code |
Tar-cf test.tar test.txt this command will package test.txt as a file named test.tar. Tar-zcf test.tar.gz test.txt this command will package test.txt and compress it into a test.tar.gz file. Tar-rf test.tar test2.txt this command will pack test2.txtinto the existing test.tar file. Tar-tf test.tar will list the files in the test.tar package. Tar-xf test.tar will be unlocked (Click here to decompress the )test.tar file. Tar-zcvf test.tar.gz test.txt is similar to tar-zcf test.tar.gz test.txt. However, this command will display the compression process. Tar-zxvf test.tar.gz will decompress the file. |
2. zip command.
General Usage: zip test.zip test.txt this command will automatically package and compress test.txt as a test.zip file.
| The Code is as follows: |
Copy code |
Zip-g test.zip test2.txt this command will compress test2.txtto the existing test.zip file. Zip-u test.zip test.txt will check that test.txt has been updated. If the file has been changed, re-compress test.txt to test.zip. Otherwise, no compression is performed. Zip-d test.zip test2.txt will delete test.zip and test2.txt. |
3. unzip is used to decompress the zip file.
General usage. Zip test.zipdecompress test.zip
Zip test.zip *. php decompress all php files in test.zip.
-F only decompress and update existing files. Other files are not pressed (but will be asked one by one when updating ).
-U decompress and update the existing files, and decompress the files that do not exist (however, you will be asked one by one when updating the files ).
-Fo: Only decompress and update existing files. Other files are not pressed (but do not ask to directly overwrite the updates ).
-Uo decompress and update an existing file, and decompress the existing file (but overwrite the update without asking ).
-O whether the file is new or old, it will be extracted from the compressed file and overwritten directly.
-N: only extract nonexistent files. Existing files are retained regardless of the old and new files.
-C is treated as the same file name (the default value is different) Regardless of Case sensitivity ).
-L all files are converted to lowercase letters during decompression.
In addition, Processing decisions when sub-directories exist:
-J no matter what the original directory is compressed, it is all unlocked in the current directory.
-D is followed by a directory name. You can specify to decompress the package to this directory.
One of the other common options:
-L only lists the files in the compressed package, and does not actually uncompress the package.
4. gzip compresses the file.
Example:
Example 1: package all the files in the/etc directory into/tmp/etc.tar
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Tar-cvf/tmp/etc.tar/etc <= package only, do not compress! [Root @ linux ~] # Tar-zcvf/tmp/etc.tar.gz/etc <= compressed with gzip [Root @ linux ~] # Tar-jcvf/tmp/etc.tar.bz2/etc <= compressed with bzip2 |
# Note that the file name after parameter f is obtained by ourselves. We use. tar for identification.
# If the z parameter is added, .tar.gz or. tgz is used to represent the tar file ~ compressed by gzip ~
# If you add the j parameter, use .tar.bz2 as the file name ~
# A warning message is displayed when the preceding command is executed:
# "Tar: Removing leading '/" from member names "is a special setting for absolute paths.
Example 2: Check the files in the above/tmp/etc.tar.gz file?
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Tar-ztvf/tmp/etc.tar.gz
|
# When we use gzip to compress the files in the tar file,
# Add the z parameter! This is important!
Example 3: Decompress the/tmp/etc.tar.gz file under/usr/local/src.
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Cd/usr/local/src [Root @ linux src] # tar-zxvf/tmp/etc.tar.gz # By default, We Can uncompress files anywhere! In this example, # First, I will transform the working directory to the/usr/local/src directory, and unlock/tmp/etc.tar.gz, # The unlocked directory will be in/usr/local/src/etc! In addition, if you enter/usr/local/src/etc # The file attributes in this directory may be different from those in/etc! |
Example 4: Under/tmp, I only want to unbind the etc/passwd in/tmp/etc.tar.gz.
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Cd/tmp [Root @ linux tmp] # tar-zxvf/tmp/etc.tar.gz etc/passwd
|
# I can use tar-ztvf to check the file name in the tarfile. If you only need one file,
# You can issue it in this way! Notice! The root directory in etc.tar.gz/is removed!
Example 5: Back up all the files in/etc/and save their permissions!
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Tar-zxvpf/tmp/etc.tar.gz/etc
|
# This-p attribute is very important, especially when you want to keep the attributes of the original file!
Example 6: only new files in/home are backed up.
| The Code is as follows: |
Copy code |
| [Root @ linux ~] # Tar-N "2005/06/01"-zcvf home.tar.gz/home |
Example 7: I want to back up/home,/etc, but not/home/dmtsai
| The Code is as follows: |
Copy code |
| [Root @ linux ~] # Tar -- exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc |
Example 8: package/etc/and unpack it under/tmp without generating a file!
| The Code is as follows: |
Copy code |
[Root @ linux ~] # Cd/tmp [Root @ linux tmp] # tar-cvf-/etc | tar-xvf-
|
# This action is a bit like cp-r/etc/tmp ~ It is still useful!
# Note that the output file is changed to-and the input file is changed to-, and there is another file | Yes ~
# This represents standard output, standard input, and pipeline commands respectively!
# In Bash shell,