11 tar command examples for creating and extracting documents in Linux

Source: Internet
Author: User

11 tar command examples for creating and extracting documents in Linux
Tar commands in Linux

The tar (Tape archiving) command is often used in the inux system to save files to an archive file.

Common file extensions include .tar.gz and .tar.bz2, which indicate further compression through gzip or bzip algorithms.

In this tutorial, we will look at some examples of daily work of creating and decompressing archive files using the tar command in Linux desktop or server version.

 

Use the tar command

The tar command is available in most linux systems by default, so you do not need to install the software separately.

The tar command has two compression 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 non-compressed archive files.

 

1.decompress A tar.gz Archive

The general method is to decompress the archive file. The command below will extract the file from a tar.gz archive file.

  1. $ tar -xvzf tarfile.tar.gz

Here is a simple explanation of these parameters-

X-decompress the file

V-lengthy mode: prints the file name when extracting each file.

Z-this file is a file compressed using gzip.

F-use the following tar archive for operations.

These are important options to remember.

Decompress the archive tar.bz2/bzip File

Files with the bz2 extension are compressed using the bzip algorithm, but the tar command can also process them, but the "z" option needs to be replaced by the "j" option.

  1. $ tar -xvjf archivefile.tar.bz2

 

2. decompress the file to a specified directory or path.

To decompress the file to a specified directory, use the "-C" option to specify the path, where "C" is capitalized "C ".

  1. $ tar -xvzf abc.tar.gz -C /opt/folder/

First, check whether the target directory exists. After all, the tar command does not create a directory for you. Therefore, if the target directory does not exist, the command will fail.

 

3. Extract A single file

To extract a single file from an archive file, you only need to place the file name after the command as follows.

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

In the preceding command, you can specify multiple files as follows.

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

 

4. Use wildcards to extract multiple files

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

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

 

5. List and retrieve the content in the tar archive file

If you only want to list the content in the tar archive file instead of decompressing it, use the "-t" (test) option, the following command is used to print the content of a tar archive file compressed by gzip.

  1. $ tar -tz -f abc.tar.gz
  2. ./new/
  3. ./new/cde.txt
  4. ./new/subdir/
  5. ./new/subdir/in.txt
  6. ./new/abc.txt
  7. ...

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

For the tar.bz2/bzip file, use the "j" option.

Use the preceding command and grep command to retrieve the archive file, as shown below. Easy!

  1. $ tar -tvz -f abc.tar.gz | grep abc.txt
  2. -rw-rw-r-- enlightened/enlightened 02015-01-1311:40./new/abc.txt

 

6. Create a tar/tar.gz archive file

Now we have learned how to decompress a tar archive file. It is time to create a new tar archive file. The tar command can be used to put the selected file or entire directory into an archive file. The following is an example.

The following command uses a directory to create a tar archive file, which adds all the files and subdirectories to the archive file.

  1. $ tar -cvf abc.tar ./new/
  2. ./new/
  3. ./new/cde.txt
  4. ./new/abc.txt

The preceding command does not create a compressed archive file. It is just a common archive file. It only puts multiple files into one archive file and does not actually compress each file.

To use compression, you can use the "z" or "j" options to perform gzip or bzip compression algorithms.

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

The file extension does not actually have any effect. “Tar.gz "and" tgz "are common extensions of gzip compression algorithms to compress files. “Tar.bz2 "and" tbz "are common extensions of files compressed by bzip compression algorithms, extension only for easy identification .).

 

7. Confirm before adding the file

A useful option is "w". This option enables the tar command to allow users to confirm each file before it is added to the archive file, which is sometimes useful.

When this option is used, only files input "y" will be added to the archive file. If you do not enter anything, the default value is "n ".

  1. # Add a specified file
  2. $ tar -czw -f abc.tar.gz ./new/*
  3. add ‘./new/abc.txt’?y
  4. add ‘./new/cde.txt’?y
  5. add ‘./new/newfile.txt’?n
  6. add ‘./new/subdir’?y
  7. add ‘./new/subdir/in.txt’?n
  8. # List all added files
  9. $ tar -t -f abc.tar.gz
  10. ./new/abc.txt
  11. ./new/cde.txt
  12. ./new/subdir/

 

8. Add a file to an existing archive file

The "r" option can be used to add an existing archive file without creating a new archive file. The following is a simple example:

  1. $ tar -rv -f abc.tar abc.txt

Files cannot be added to compressed archive files (gz or bzip ). Files can only be added to common archive files.

 

9. Add the file to the compressed archive file (tar.gz/tar.bz2)

It has been mentioned before that it is impossible to add files to compressed archive files. However, it can still be done through some simple tricks. Use the gunzip command to decompress the archive file, add the file to the archive file, and then compress it.

  1. $ gunzip archive.tar.gz
  2. $ tar -rf archive.tar ./path/to/file
  3. $ gzip archive.tar

Bzip2 and bunzip2 are used for bzip files.

 

10. Back up data using tar

A real scenario is to back up directories at a fixed interval. the tar command can implement such a backup through cron scheduling. The following is an example:

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

Running the preceding command using cron will keep creating a backup file named 'archive-20150218.tar.gz '.

Of course, it is necessary to ensure that the increasing number of archive files does not cause disk space overflow.

 

11. Verify when creating an archive file

The "W" option can be used for verification after an archive file is created. The following is a simple example.

  1. $ tar -cvW -f abc.tar ./new/
  2. ./new/
  3. ./new/cde.txt
  4. ./new/subdir/
  5. ./new/subdir/in.txt
  6. ./new/newfile.txt
  7. ./new/abc.txt
  8. Verify./new/
  9. Verify./new/cde.txt
  10. Verify./new/subdir/
  11. Verify./new/subdir/in.txt
  12. Verify./new/newfile.txt
  13. Verify./new/abc.txt

Note that the verification action cannot be performed on the compressed archive file, but only on the non-compressed tar archive file.

This is the first time. You can run the "man tar" command to view the manual of the tar command.

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.