Compressed and packaged command-Tar

Source: Internet
Author: User

When pulling logs today, I suddenly found several logs to be pulled, so I began to package the logs.

Ls app-gateway.log.1 {4 .. 8} [the log to be intercepted is these]

App-gateway.log.14 app-gateway.log.15 app-gateway.log.16 app-gateway.log.17

Tar-czvf app-gateway.log.1.0809.tar.gz app-gateway.log.1 {4 .. 8}[Use tar command to compress]

App-gateway.log.14
App-gateway.log.15
App-gateway.log.16
App-gateway.log.17
App-gateway.log.18

Or use the following command:

Zip-r app-gateway.log.1.0809.zip app-gateway.log.1 {4 .. 8} [Use zip command to compress]

Adding: app-gateway.log.14 (deflated 90%)
Adding: app-gateway.log.15 (deflated 90%)
Adding: app-gateway.log.16 (deflated 90%)
Adding: app-gateway.log.17 (deflated 90%)
Adding: app-gateway.log.18 (deflated 90%)

Let's summarize:

Tar is a classic Unix Command that has been transplanted to Linux. Tar is tape archive (Tape ArchivingIs initially designed to package files on tape. If you have downloaded the Linux source code, you may have encountered a tar file. This is a file-based command, which essentially stacks files consecutively and at the beginning and end.

You can use tar to package the entire directory tree, which makes it especially suitable for backup. You can restore all the archive files or expand separate files and directories from them. Backups can be saved to file-based devices or tape devices. Files can be redirected during restoration to put them in a directory (or system) different from the directory (or system) where they were originally saved ). Tar is irrelevant to the file system. It can be used on ext2, ext3, JFS, Reiser, and other file systems.

Using tar is very similar to using a file utility such as PKZIP. You only need to point it to a target (a file or device) and specify the file you want to package. You can use the standard compression type to dynamically compress an archive file, or specify an external compression program of your choice. To use Bzip2 to compress or decompress a file, run the tar-Z command.

Syntax: Tar (main option + auxiliary option) file or directory

When you use this command, the main option is required, which tells tar what to do, and the auxiliary option is optional for auxiliary use.

Main options:

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 options:

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, you can compress the file, but the file is also compressed during restoration.

Use this option to decompress the package.

Example 1: create a full directory file named usr.tar.

$ TarCVFUsr.tar/home

Example 2: make a backup file and compress all the subdirectories in the/home directory. The backup file name isUsr.tar.gz.

$ TarCzvfUsr.tar.gz/home

Example 3: backup file usr.tar.gzRestore and decompress.

$ Tar xzvf usr.tar.gz

Example 4: unzip usr.tar to back up the file andSplit screen modeDisplay on the monitor.

$ TarTvfUsr.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: On the/dev/fd0 deviceCreate a backup file in a floppy diskAnd all the files in the/Home Directory

Copy to the backup file.

$ TarCf/Dev/fd0/home

To restore files on a device disk, use the xf option:

$ TarXF/Dev/fd0

If the size of the backup file exceeds the storage space available for 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.

During archiving, the tar command reminds you to put a new floppy disk when a floppy disk is full. 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 disk into the soft drive, and then enter the tar command with the X and m options. In

You will be reminded to add another floppy disk when necessary.

$ Tar xmf/dev/fd0

Example 6: use tar to back up the entire file system except the/proc directory to the SCSI tape device.

Tar-CPF/dev/st0/
-- Exclude =/proc

In the preceding example, the-C switch indicates that the archive file is being created. The-P switch indicates that we want to retain the file license permission, which is critical for good backup. -F indicates the file name of the archive object. In this example, we use the original tape device/dev/st0. /Indicates the content we want to back up. Since we want to back up the entire system, we need to specify this toggle as the root ). When you point tar to a directory/
It is automatically recursive. Finally, the/proc directory is excluded because it does not contain any content to be saved. If a single tape cannot accommodate this backup, we need to add the-M switch (not shown in this example) for multi-volume backup.

To restore one or more files, use the tar command with the extract switch (-x:

Tar-xpf/dev/st0-C/

Here, the-F switch also points to the archive file. The-P switch indicates that we want to restore the archive permission. -X indicates that the file is extracted from the archive. -C/indicates that we want to start the restoration from. Tar is usually restored to the directory where the command is run. The-C switch makes our current directory no longer relevant.

The other two tar commands you may frequently use are-T and-D. The-T switch is used to list the content of an archive file. The-D switch compares the content of the archive file with the current file on the system.

For ease of operation and editing, you can put the file and directory you want to archive into a text file, and then reference the text file through the-T switch on the command line. These files and directories can be combined with other directories listed on the command line. The following command line backs up all files and directories listed in myfiles, the/root directory, and all ISO files in the/tmp directory.

Tar-CPF/dev/st0-T myfiles/root/tmp/*. ISO

The file list is just a text file that lists the files or directories. The following is an example:

/Etc

/Var

/Home

/Usr/local

/OPT

Note that the tar-T (or files-from) command cannot accept wildcards. Files must be explicitly listed. The above example shows a method to reference a file separately. You can also execute scripts to search for the system and create a list. The following is an example of a script:

#! /Bin/sh

Cat myfiles> templist

Find/usr/share-INAME *. PNG> templist

Find/tmp-INAME *. ISO> templist

Tar-cpzmf/dev/st0-T templist

The above script first copies the list of all existing files in myfiles to templist. Then it executes two FIND commands to search for files matching a certain pattern in the file system and attaches them to templist. The first time is to search for all files ending with. PNG in the/usr/share directory tree. The second is to search for all files ending with. ISO in the/tmp directory tree. After the list is created, tar then creates a new archive file on the file device/dev/st0 (the first SCSI tape device ).
ZIP format to compress and retain all file permissions. The archive file spans multiple volumes. The name of the file to be archived is extracted from the templist file.

You can also use scripts to perform more detailed operations, such as Incremental backup. You can also write scripts to restore files, although the restoration is usually done manually. As mentioned above, the-x switch used to extract Files Replaces the-C switch. You can restore an entire archive file or a specified file or directory. You can use wildcards to reference files in an archive file. You can also use the switch to dump and restore data.

Example: 1. Package all the files in the/home/www/images directory as/home/www/images.tar.

[Root @ xoaocom ~] # Tar-CVF/home/www/images.tar/home/www/images packages are only packaged and not compressed

[Root @ xoaocom ~] # Tar-zcvf/home/www/images.tar.gz/home/www/images compressed with Gzip

The name of the compressed file after the fzip file is obtained by using the tar.gz or tgz.

Example 2: extract/home/www/images.tar.gz to/home/WWW

[Root @ xoaocom ~] # Cd/home/WWW

[Root @ xoaocom ~] # Tar-zxvf/home/images.tar.gz

Decompress the package to the specified directory.

[Root @ xoaocom ~] # Tar-zxvf/home/images.tar.gz-C/specific-Dir

Unpackage to the specified directory

Tar xvf filename.tar-C/specific-Dir



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.