tutorial on using the TAR command to compress and decompress files in a Linux system

Source: Internet
Author: User
Tags bz2 time interval

The tar command in Linux

The TAR (tape archive) command is a command that is often used in Linux systems to deposit files into an archive file.

The common file extensions include:. tar.gz and. tar.bz2, respectively, to express the further compression through the GZIP or bzip algorithm.

In this tutorial, we'll take a look at the daily work of creating and glimpse files using the tar command on the Linux desktop or server version.

Using the TAR command

The tar commands are available by default in most Linux systems, so you don't have to install the software separately.

The TAR command has two compressed formats, gzip and bzip, the "z" option of this command is used to specify gzip, and the "j" option is used to specify bZIP. You can also create a uncompressed archive.

1. Extract a tar.gz archive

The common usage is to extract the archive, and the following command extracts the file from a tar.gz archive.

The code is as follows:

$ TAR-XVZF tarfile.tar.gz

Here's a simple explanation for these parameters-

X-Extract Files

V-verbose mode, which prints out the name of the file as each file is decompressed.

Z-This file is a file that is compressed using gzip.

F-Use the next tar archive for the operation.

These are some of the important options to remember.

Extract Tar.bz2/bzip archive files

Files with bz2 extensions are compressed using the bZIP algorithm, but the TAR command can also process them, but you need to replace the "z" option by using the "j" option.

The code is as follows:

$ TAR-XVJF archivefile.tar.bz2

2. Extract the file to a specified directory or path

In order to extract the file into a specified directory, use the "-C" option to specify the path, where "C" is capitalized "C".

The code is as follows:

$ TAR-XVZF abc.tar.gz-c/opt/folder/

Then, you first need to verify that the target directory exists, after all, the tar command does not create a directory for you, so if the target directory does not exist, the command fails.

3. Extract a single file

To extract a single file from an archive, simply place the file name behind the command in the following way.

The code is as follows:

$ tar-xz-f abc.tar.gz "./new/abc.txt"

In the preceding command, you can specify multiple files in the following ways.

The code is as follows:

$ tar-xz-f abc.tar.gz "./new/cde.txt" "./new/abc.txt"

4. Use wildcard characters to extract multiple files

Wildcards can be used to extract a batch of files that match a given wildcard character, such as all files with ". txt" as the extension.

The code is as follows:

$ tar-xz-f abc.tar.gz--wildcards "*.txt"

5. List and retrieve the contents of the TAR Archive

If you just want to list and not extract the contents of the TAR archive, use the "-t" option, and the following command is used to print the contents of a tar archive that uses gzip compression.

The code is as follows:

$ tar-tz-f abc.tar.gz







The output can be piped to grep to search for a file, or directed to the less command to browse the content list. Using the "V" verbose option will print out additional details for each file.

For tar.bz2/bzip files, you need to use the "j" option.

Retrieve the archive with the above command and grep command, as shown below. It's easy!

The code is as follows:

$ tar-tvz-f abc.tar.gz | grep abc.txt

-rw-rw-r--enlightened/enlightened 0 2015-01-13 11:40./new/abc.txt

6. Create a tar/tar.gz archive

Now that we've learned how to extract a tar archive, it's time to start creating a new tar archive. The tar command can be used to place the selected file or entire directory in an archive file, and the following is the appropriate example.

The following command uses a directory to create a tar archive that adds all the files and subdirectories in the directory to the archive.

The code is as follows:

$ TAR-CVF Abc.tar./new/




The above command does not create a compressed archive, just a normal archive, just putting multiple files into an archive and not really compressing each file.

To use compression, you can use either the "Z" or "J" options for Gzip or bzip compression algorithms.

The code is as follows:

$ tar-cvzf abc.tar.gz./new/

The extension of the file does not actually have any effect. "Tar.gz" and "tgz" are common extensions of the gzip compression algorithm compressed files. "tar.bz2" and "tbz" are common extensions of bzip compression algorithms (LCTT: Whether the archive is compressed and which compression method is not dependent on its extension, the extension is for easy identification.) )。

7. Confirm before adding the file

A useful option is "w", which allows the tar command to be validated by adding each file to the archive, which can sometimes be useful.

When this option is used, only the file that the user enters "Y" is added to the archive, and if you do not enter anything, the default is an "n".

The code is as follows:

# Add specified file

$ tar-czw-f abc.tar.gz./new/*

Add './new/abc.txt '? y

Add './new/cde.txt '? y

Add './new/newfile.txt '? n

Add './new/subdir '? y

Add './new/subdir/in.txt '? n


$ tar-t-F abc.tar.gz




8. Join the file into the existing archive file

The "r" option can be used to add files to an existing archive without creating a new archive, and here's a simple example:

The code is as follows:

$ tar-rv-f Abc.tar Abc.txt

The file cannot be added to the compressed archive (GZ or bzip). The file can only be added to the normal archive file.

9. Add files to a compressed archive (TAR.GZ/TAR.BZ2)

It has already been mentioned that it is not possible to add files to a compressed archive, but it can still be done with some simple tricks. Use the Gunzip command to extract the archive, then add the file to the archive and then compress it again.

The code is as follows:

$ gunzip archive.tar.gz

$ tar-rf Archive.tar./path/to/file

$ gzip Archive.tar

For bzip files, use bzip2 and BUNZIP2 respectively.

10. Backup via Tar

A real scenario is to back up the directory at a fixed time interval, and the tar command can use a cron schedule to do a backup like this, and here's a sample:

The code is as follows:

$ tar-cvz-f archive-$ (date +%y%m%d). tar.gz./new/

Using Cron to run the above command keeps creating a backup file that resembles the following name: ' Archive-20150218.tar.gz '.

Of course, you need to ensure that the growing archive does not cause disk space to overflow.

11. Validation when creating archive files

The "W" option can be used to validate after the archive is created, as follows is a simple example.

The code is as follows:

$ tar-cvw-f Abc.tar./new/













It should be noted that the validation action cannot be performed on a compressed archive and can only be performed on a uncompressed tar archive file.

That's all for now, and you can check the tar command's manual by using the "Man tar" command.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.