Linux tar command is a powerful weapon to archive or distribute files. GNU tar archive package can contain multiple files and directories, but also retain their file permissions, it also supports multiple compression formats. Tar represents "Tape Archiver", this format is POSIX standards.
Tar file format
tar compression levels Description:
No compression is not compressed file ending with .tar.
Gzip Gzip compressed tar format is the most widely used compression formats, it can quickly compress and extract files. Compressed with gzip files usually end with .tar.gz or .tgz. Here are some examples of how to create and extract the tar.gz file.
Bzip2 and Gzip compression format compared Bzip2 provides better compression ratio. Create a compressed file is relatively slow, usually .tar.bz2 end.
Lzip (LAMA) Gzip compression Lizp compression combines the advantages of fast, well and Bzip2 similar (or even better) compression ratio. Despite these advantages, this format is not widely used.
Lzop Compression This option may be compressed tar fastest compression format, and its compression ratio is similar to gzip, but is not widely used.
Common formats are tar.gz and tar.bz2. If you want to quickly compress, then it is to use gzip. If the archive size is more important, is to use tar.bz2.
tar command used to do?
Here are some common situations using the tar command.
Backup server or desktop system
Most Linux systems are installed by default tar. If not, there is the installation tar command.
In CentOS, the root user to execute the following command to install the shell tar.
yum install tar
The following command installs the tar on Ubuntu. "Sudo" command ensures that apt to run commands with root privileges.
sudoapt-get install tar
The following apt command to install tar on Debian.
apt-get install tar
tar command can also be used in Windows, you can download it from Gunwin project http://gnuwin32.sourceforge.net/packages/gtar.htm in.
Create tar.gz file
Below are some examples of the tar command running in the shell. Here I will explain these command-line options.
tar pczf myarchive.tar.gz / home / till / mydocuments
This command creates the archive myarchive.tar.gz, including the path / home / till / mydocuments files and directories. Command line options are explained:
[P] This option means "preserve", which indicates that retention tar file ownership and permissions information in the archive.
[C] represents the creation. This option can not be missing when you want to create the file.
[Z] z option is enabled gzip compression.
[F] file option tells tar to create an archive. Without this option tar output will be sent to standard output (LCTT Annotation: If not specified, the default is standard output screen, you obviously do not want to show a bunch of gibberish on the screen, you can usually use a pipe symbol to go to other programs ).
Tar command examples
Example 1: backup / etc directory
Create / etc configure a backup directory. Is saved in root directory.
tar pczvf /root/etc.tar.gz / etc
Use tar to back up the / etc directory
To root user run the command to ensure that all of the files in / etc will be included in the backup. This time, I added the [v] option to the command. This option means verbose, it tells tar to display all be included in the archive file name.
Example 2: Backup your / home directory
Create a backup of your home directory. The backup will be saved to the / backup directory.
tar czf /backup/myuser.tar.gz / home / myuser
Replace myuser with your user name. This command, I omit the [p] option, will not save the permissions.
Example 3: file-based MySQL database backup
In most Linux distributions, MySQL database stored in / var / lib / mysql. You can use the following command to view:
ls / var / lib / mysql
MySQL using tar file-based backup
Use tar to back up MySQL data files in order to maintain data consistency, first deactivate the database server. The backup will be written to / backup directory.
1) Create a backup directory
mkdir / backup
chmod600 / backup
2) Stop MySQL, backup with tar and restart the database.
service mysql stop
tar pczf /backup/mysql.tar.gz / var / lib / mysql
service mysql start
ls-lah / backup
MySQL-based backup files
Extract tar.gz file
Extract tar.gz file command is:
tar xzf myarchive.tar.gz
tar command options explained
[X] x represents the extraction, the order indispensable when extracting tar files.
[Z] z option tells tar to extract the archive file is gzip format.
[F] This option tells tar to read the contents of an archive from a file, the present case is myarchive.tar.gz.
The above tar command quietly extract tar.gz file, unless there is an error message. If you want to see what files are extracted, then add a "v" option.
tar xzvf myarchive.tar.gz
[V] option means verbose, it will show you extract the file name.
Extract tar.gz file