Summary of using skills of tar command in Linux system

Time:2020-10-20

The Linux tar command is a powerful weapon when archiving or distributing files. The GNU tar archive can contain multiple files and directories, retain its file permissions, and support multiple compression formats. Tar stands for “tape archiver”, which is the POSIX standard.
Tar file format
Introduction to tar compression level:

Uncompressed, uncompressed files end with. Tar.
Gzip compression gzip format is the most widely used compression format for tar, which can quickly compress and extract files. Gzip compressed files are usually used tar.gz Or. Tgz. Here’s how to create and unzip tar.gz Examples of documents.
Bzip2 compression provides better compression ratio than gzip format. Creating compressed files is also slow, usually using tar.bz2 ending.
Lzip (Lama) compression lizp compression combines the advantages of gzip’s speed and compression rates similar to or better than bzip2. Despite these benefits, the format has not been widely used.
Lzop compression is probably the fastest compression format in tar. It has a compression ratio similar to gzip, but it is not widely used.
The common format is tar.gz And tar.bz2 。 If you want fast compression, use gzip. If the size of the archive file is important, the tar.bz2 。

What is the tar command for?
Here are some common scenarios for using the tar command.

Backup server or desktop system
Document archiving
Software distribution

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

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

Copy code

The code is as follows:

yum install tar

Ubuntu
The following command will install tar on Ubuntu. The “sudo” command ensures that the apt command runs with root privileges.

Copy code

The code is as follows:

sudo apt-get install tar

Debian
The following apt command installs tar on Debian.

Copy code

The code is as follows:

apt-get install tar

Windows
The tar command is also available in windows, you can use the gunwin project from http://gnuwin32.sourceforge.net/packages/gtar.htm Download it from.

establish tar.gz file
Here are some examples of running the tar command in a shell. I’ll explain the command line options below.

Copy code

The code is as follows:

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

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

[p] The “preserve” option indicates that it belongs to the archive file.
[c] Represents 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. If you don’t have this option, tar will send the output to standard output.

Tar command example
Example 1: backing up the / etc directory

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

Copy code

The code is as follows:

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

2015128110935163.png (988×452)

To run the command as root, make sure that all files in / etc are included in the backup. This time, I added the [v] option to the command. This option represents verb, which tells tar to display the names of all files 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.

tar czf /backup/myuser.tar.gz /home/myuser
Replace myuser with your user name. In this command, I omit the [P] option, so the permissions will not be saved.

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:

Copy code

The code is as follows:

ls /var/lib/mysql

2015128110955732.png (675×425)

When using tar to backup MySQL data files, in order to maintain data consistency, the database server is first disabled. The backup is written to the / backup directory.

1) Create backup directory

Copy code

The code is as follows:

mkdir /backup
chmod 600 /backup

2) Stop mysql, back up with tar and restart the database.

Copy code

The code is as follows:

service mysql stop
tar pczf /backup/mysql.tar.gz /var/lib/mysql
service mysql start
ls -lah /backup

2015128111013606.png (675×425)

Extraction tar.gz file
Extraction tar.gz The command for the file is:

Copy code

The code is as follows:

tar xzf myarchive.tar.gz

Explanation of tar command options
[x] X represents extraction, which is indispensable when extracting tar files.
[z] The Z option tells tar that the archive to be unzipped is in gzip format.
[f] This option tells tar to read the contents of the archive from a file, in this case myarchive.tar.gz 。
The tar command above will be quietly extracted tar.gz File, unless there is an error message. If you want to see which files have been extracted, add the “V” option.

Copy code

The code is as follows:

tar xzvf myarchive.tar.gz

[v] Option means verb, which will show you the name of the extracted file.
2015128111039230.png (731×425)

Recommended Today

Application of tree

Application of tree Storage structure of tree Parental representation (sequential storage) Definition: in each node, the “pointer” (position subscript) pointing to the parents is saved. The root node is fixedly stored in 0, – 1 indicates that there are no parents. Add: add directly without following the logical order. Delete: ① set the pointer to […]