Summary of the usage techniques of the TAR command under Linux system

Source: Internet
Author: User
Tags bz2 file size gz file centos file permissions mysql database

The Linux tar command is a powerful weapon for filing or distributing files. The GNU tar Archive package can contain multiple files and directories, retains its file permissions, and supports multiple compression formats. Tar represents "Tape archiver", a format that is POSIX-compliant.

Tar file format

Brief introduction of tar compression level:

Files without compression without compression end with. Tar.

The gzip compressed gzip format is the most widely used compression format for tar, which enables fast compression and extraction of files. Files that are compressed with gzip usually end with the. tar.gz or. tgz. Here are some examples of how to create and decompress tar.gz files.

BZIP2 compression and Gzip formats provide a better compression ratio than BZIP2. Creating a compressed file is also slow, usually with a. tar.bz2 end.

Lzip (LAMA) compression LIZP compression combines Gzip-fast advantages with a Bzip2-like (or even better) compression rate. Despite these benefits, this format has not been widely used.

Lzop compression This compression option may be the fastest compression format for tar, its compression rate and gzip similar, but also not widely used.

The common format is tar.gz and tar.bz2. If you want to compress quickly, then use gzip. If the file size is more important, use tar.bz2.

What is the tar command for?

Here are some common scenarios for using the TAR commands.

Backup server or desktop system

Document archiving

Software distribution

Install tar

Most Linux systems have tar installed by default. If not, here is the command to install tar.

Centos

In CentOS, install tar as the root user executes the following command in the shell.

Copy Code

The code is as follows:

Yum Install tar

Ubuntu

The following command installs tar on Ubuntu. The sudo command ensures that the APT command runs with root privileges.

The code is as follows:

sudo apt-get Install tar

Debian

The APT command below installs tar on Debian.

The code is as follows:

Apt-get Install tar

Windows

The TAR command is also available in Windows and you can download it from the Gunwin project http://gnuwin32.sourceforge.net/packages/gtar.htm.

Creating tar.gz Files

Here are some examples of running the tar command in a shell. I will explain these command-line options below.

The code is as follows:

Tar pczf myarchive.tar.gz/home/till/mydocuments

This command creates the archive file myarchive.tar.gz, which includes the files and directories in the path/home/till/mydocuments. Command line option Explanation:

[P] This option represents "preserve", which instructs Tar to retain the file owner and permission information in the archive.

[C] indicates creation. This option is not missing when you want to create a file.

The [z] Z option enables gzip compression.

[f] The file option tells Tar to create an archive file. Without this option, tar will send the output to the standard output (LCTT: If not specified, the standard output defaults to the screen, you obviously do not want to display a bunch of garbled on the screen, usually you can use the pipe symbol to send to other programs).

Sample Tar command

Example 1: Backing Up the/ETC directory

Create a backup of the/ETC configuration directory. The backup is saved in the root directory.

The code is as follows:

Tar pczvf/root/etc.tar.gz/etc

To run the command as root, ensure that all files in the/etc are included in the backup. This time, I added the [v] option to the command. This option represents verbose, which tells Tar to display all file names that are included in the archive file.

Example 2: Back up your/home directory

Create a backup of your home directory. The backup is saved to the/backup directory.

Tar Czf/backup/myuser.tar.gz/home/myuser

Replace the myuser with your username. In this command, I omit the [p] option and do not save the permissions.

Example 3: file-based MySQL Database backup

In most Linux distributions, the MySQL database is stored in/var/lib/mysql. You can use the following command to view:

The code is as follows:

Ls/var/lib/mysql

To keep data consistent when backing up MySQL data files with tar, first deactivate the database server. The backup will be written to the/backup directory.

1) Create the backup directory

The code is as follows:

Mkdir/backup

chmod 600/backup

2 stop MySQL, use tar to back up and restart the database.

The code is as follows:

Service MySQL Stop

Tar Pczf/backup/mysql.tar.gz/var/lib/mysql

Service MySQL Start

Ls-lah/backup

Extract tar.gz files

The command to extract the tar.gz file is:

The code is as follows:

Tar xzf myarchive.tar.gz

Tar command options explanation

[x] x indicates extraction, and this command is indispensable when extracting the tar file.

The [z] Z option tells Tar that the archive to extract is in gzip format.

[f] This option tells Tar to read the archived content from a file, in this case myarchive.tar.gz.

The tar command above will silently extract the tar.gz file unless there is an error message. If you want to see which files are extracted, add the "V" option.

Copy Code

The code is as follows:

Tar xzvf myarchive.tar.gz

The [v] option indicates verbose, which shows you the extracted file name.

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.