Basic Linux Tutorial: tar command usage

Source: Internet
Author: User

Basic Linux Tutorial: tar command usage

The Linux tar command is a powerful tool for archiving or distributing files. The GNU tar archive package can contain multiple files and directories and retain the file permissions. It also supports multiple compression formats. Tar indicates "Tape Archiver". This format is POSIX standard.

 

Tar file format

Tar compression level introduction:

  • Files that are not compressed and not compressed end with. tar.
  • Gzip is the most widely used compression format for tar. It can quickly compress and Extract files. Files compressed with gzip usually end with .tar.gz or. tgz. Here are some examples of how to create and decompress the tar.gz file.
  • Bzip2 provides a better compression ratio than Gzip. It is also slow to create a compressed file, usually ending with .tar.bz2.
  • Lzip (LAMA) compression Lizp compression combines the advantages of Gzip fast, and compression ratio similar to Bzip2 (or even better. Despite these benefits, this format is not widely used.
  • Lzop compression, which may be the fastest compression format of tar, has a similar compression ratio as gzip, but is not widely used.

The common formats are tar.gz and tar.bz2. If you want to compress quickly, gzip is used. If the size of the archive file is important, use tar.bz2.

 

What is the tar command used?

The following are some common cases of using the tar command.

  • Backup Server or desktop system
  • Document Archiving
  • Software Distribution

 

Install tar

Tar is installed in most Linux systems by default. If not, run the tar installation command.

 

CentOS

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

  1. yum install tar

 

Ubuntu

The following command installs tar on Ubuntu. The "sudo" command ensures that the apt command runs with the root permission.

  1. sudoapt-get install tar

 

Debian

The following apt command installs tar On Debian.

  1. apt-get install tar

 

Windows

The tar command can also be used in Windows. You can download the keystore from the Gunwin project http://gnuwin32.sourceforge.net/packages/gtar.htm.

 

Create a tar.gz File

Below are some examples of running the tar command in shell. Below I will explain these command line options.

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

This command will create the archive file myarchive.tar.gz, including the files and directories in the path/home/till/mydocuments. Command line options:

  • [P] This option indicates "preserve", which indicates that tar retains the file owner and permission information in the archive file.
  • [C] indicates creation. This option cannot be missing when creating a file.
  • [Z] The 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 it is not specified, the standard output is the screen by default. Obviously, you will not want to display a bunch of garbled characters on the screen, generally, you can use pipeline symbols to send them to other programs ).

 

Tar command example

Example 1: backup/etc directory

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

  1. tar pczvf /root/etc.tar.gz /etc

Back up the/etc directory with tar

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

Example 2: Back up your/home Directory

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

  1. tar czf /backup/myuser.tar.gz /home/myuser

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

Example 3: file-based MySQL Database Backup

In most Linux releases, MySQL databases are stored in/var/lib/mysql. You can use the following command to View Details:

  1. ls/var/lib/mysql

Use tar to back up MySQL based on files

To maintain data consistency when backing up MySQL Data Files with tar, the database server is disabled first. The backup will be written to the/backup directory.

1) create a backup directory

  1. mkdir/backup
  2. chmod600/backup

2) Stop MySQL, use tar for backup, and restart the database.

  1. service mysql stop
  2. tar pczf /backup/mysql.tar.gz /var/lib/mysql
  3. service mysql start
  4. ls-lah /backup

File-based MySQL backup

 

Extract the tar.gz File

The command to extract the tar.gz file is:

  1. tar xzf myarchive.tar.gz

 

Tar command option explanation
  • [X] x indicates extraction. This command is indispensable when the tar file is extracted.
  • [Z] The z option tells tar that the archive file to be decompressed is in gzip format.
  • [F] This option tells tar to read archive content from a file. In this example, It is myarchive.tar.gz.

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

  1. tar xzvf myarchive.tar.gz

The [v] Option indicates verbose, which displays the extracted file name.

Extract the tar.gz File

Via: https://www.howtoforge.com/tutorial/linux-tar-command/

Author: howtoforge Translator: ictlyh Proofreader: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

This article permanently updates the link address:

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.