Detailed usage of tar, gzip, and unzip commands

Source: Internet
Author: User
For details about tar, gzip, and unzip commands, refer to tar to create files and directories. Using tar, you can create a file (backup file) for a specific file, change the file in the file, or add a new file to the file. Tar is used to create detailed usage of tar, gzip, and unzip commands on tape. you can use tar to create files and directories. Using tar, you can create a file (backup file) for a specific file, change the file in the file, or add a new file to the file. Tar was originally used to create files on tape. now, you can create files, such as floppy disks, on any device. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission. Tar on Linux is of the GNU version. Syntax: when you use this command in a tar [primary option + secondary option] file or directory, the primary option is required. it tells tar what to do and the secondary option is used, optional. Main option: c. create a new archive file. Select this option if you want to back up a directory or some files. R: append the file to the end of the file. For example, if you have prepared a backup file and find that there is still a directory or some files have forgotten to be backed up, you can use this option to append the directory or files you have forgotten to the backup file. T list the file content and check which files have been backed up. U updates the file. That is to say, replace the original backup file with the new file. if the file to be updated cannot be found in the backup file, append it to the end of the backup file. X release a file from the file. Auxiliary option: B. This option is set for the tape drive. It is followed by a number to describe the block size. the default value is 20 (20*512 bytes ). F. This option is usually required when you use an archive file or device. K. Save the existing files. For example, if we restore a file, the same file will not be overwritten during restoration. M sets the modification time of all files to the present when restoring files. M creates a multi-volume archive file to store it on several disks. V detailed report on the file information processed by tar. If this option is not available, tar does not report file information. W each step requires confirmation. Z uses gzip to compress/decompress the file. With this option, the file can be compressed. However, you must use this option to decompress the file during restoration. Example 1: Create a full-part sub-directory file named usr.tar in the/home directory. $ Tar cvf usr.tar/home Example 2: create a full file of the sub-directories in the/home directory and compress the file with the name usr.tar.gz. $ Tar czvf usr.tar.gz/home Example 3: restore and decompress the backup file usr.tar.gz. $ Tar xzvf usr.tar.gz Example 4: The contents of the usr.tar backup file are displayed on the display in split screen mode. $ Tar tvf usr.tar | more to back up a file to a specific device, you only need to use the device name as the backup file name. Example 5: You can create a backup file in a floppy disk of the/dev/fd0 device and copy all the files in the/home directory to the backup file. $ Tar cf/dev/fd0/home to restore files on the device disk, you can use the xf option: $ tar xf/dev/fd0 if the size of the file backed up by the user exceeds the available storage space of the device, such as a floppy disk, you can create a multi-volume tar backup file. The M option indicates that the tar command prompts you to use a new storage device. when you use the M option to archive data to a soft Drive, when a floppy disk is full, the tar command reminds you to add a new floppy disk. In this way, you can save the tar file to several disks. $ Tar cMf/dev/fd0/home: to restore the files on several disks, you only need to put the first file on the soft Drive and enter the tar command with the x and M options. If necessary, you will be reminded to add another floppy disk. The $ tar xMf/dev/fd0 gzip command reduces the file size by two obvious advantages: one is to reduce the storage space, and the other is to reduce the transmission time when the file is transmitted over the network. Gzip is a frequently used command in Linux to compress and decompress files, which is convenient and easy to use. Syntax: gzip [option] indicates the meaning of each option in the compressed (decompressed) File name:-c writes the output to the standard output and retains the original file. -D. decompress the compressed file. -L the following fields are displayed for each compressed file: compressed file size: uncompressed file size compression ratio: uncompressed file name-r recursively search for a specified directory and compress all 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. Assume that a directory/homehas the mm.txtw.sort.txt and xx.com files. Example 1: compress each file in the/homecatalog into A. gz file. $ Cd/home $ gzip * $ ls m.txt.gz sort.txt.gz xx.com.gz Example 2: extract each compressed file in example 1 and list the detailed information. $ Gzip-dv * mm.txt.gz 43.1% ----- replaced with mm.txt sort.txt.gz 43.1% ----- replaced with sort.txt xx.com.gz 43.1% ----- replaced with xx.com $ ls mm.txt sort.txt xx.com Example 3: detailed information of each compressed file in Example 1 is displayed without decompression. $ Gzip-l * compressed uncompr. ratio uncompressed_name 277 445 43.1% mm.txt 278 445 43.1% sort.txt 277 445 43.1% xx.com $ ls mm.txt.gz sort.txt.gz xx.com.gz Example 4: to compress a Tartar file, for example, usr.tar. the extension name of the compressed file is .tar.gz $ gzip usr.tar $ ls usr.tar.gz unzip. how can we use the Microsoft Windows compression software winzip to compress the file in Linux? You can use the unzipcommand to unexpand a compressed file named. zip. Syntax: unzip [option] the options of the compressed file name. ZIP are described as follows:-x file list to decompress the file, but do not include the specified file. -V. -T whether the test file is damaged, but the pressure is not solved. -D Directory: decompress the compressed file to the specified directory. -Z only displays the annotation of the compressed file. -N does not overwrite existing files. -O overwrites existing files and does not require user confirmation. -J. do not recreate the directory structure of the document and decompress all the files to the same directory. Example 1: extract text.zip from the current directory. $ Unzip text.zip Example 2: extract the compressed file text.zip under the specified directory/tmp. if the same file already exists, the unzip command must not overwrite the original file. $ Unzip-n text.zip-d/tmp Example 3: view the Directory of the compressed file, but the file cannot be compressed. $ Unzip-v text.zip zgrep command the function of this command is to find a matching regular expression in the compressed file. its usage is the same as that of the grep command, except that the object operated is a compressed file. You can use the zgrep command to check whether a certain sentence exists in a compressed file.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.