17 Linux Tar Command Practical example _linux

Source: Internet
Author: User
Tags bz2 compress archive gpg

Tar (Tape ARchive, abbreviation for tape archive, LCTT originally designed to package files onto tape, and now most of us use it to back up a partition or some important directory) is the most widely used command in Unix-like systems, Used to archive multiple files or directories into a single archive, and the archive can be further compressed using technology such as gzip or bzip2. In other words, the tar command can also be used for backup: First archive multiple files and directories to a separate tar file or archive, and then release files and directories from the tar file as needed.

This article describes the 17 practical examples of tar.

The tar command syntax is as follows:

# Tar < options > < file >

The following are some of the commonly used options in the tar command:

--delete: Remove from archive (not tape)

-R,--append: Append files to archive

-T,--list: Listing what is contained in the archive

--test-label: Test Archive volume label and exit

-U,--update: Append updated files to archive

-X,--extract,--get: Releasing files and directories in archive files

-C,--directory=dir: Change the working directory to DIR before performing the archive action

-F,--file=archive: Specifies the archive file name (that will be created or existing)

-j,--BIP2: Using bzip2 compression for archive files

-j,--XZ: Using XZ compression for archive files

-P,--preserve-permissions: Preserve access to the original file

-V,--verbose: Display command entire execution process

-Z, gzip: Using gzip compression for archive files

Note: the connector in the TAR command option-is optional (LCTT: No-it's okay.) This is rare in GNU software, presumably because the tar commands are more influenced by the old UNIX style.

Example 1: Create a tar archive

Now to create a tar file, package the/etc/directory and the/root/anaconda-ks.cfg file.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-CVF myarchive.tar/etc/root/anaconda-ks.cfg

The above command creates a tar file named "Myarchive" in the current directory, containing/etc/directories and/root/anaconda-ks.cfg files.

Where the-C option indicates that the tar file is to be created, the-v option is used to output the detailed process of tar to the screen, and the-f option is to specify the archive file name.

 [Root@linuxtechi ~]# ls-l myarchive.tar
-rw-r--r--. 1 root 22947840 Sep 7 00:24 myarchive.tar
[Root@linuxtec Hi ~]# 

Example 2: List The contents of the archive file

Using the –T option in the Tar command lets you quickly list what is contained in a file without releasing the files.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-TVF Myarchive.tar

Lists the files and directories specified in the tar file. The following command attempts to see if the Anaconda-ks.cfg file exists in the tar file.

 [Root@linuxtechi ~]# TAR-TVF myarchive.tar root/anaconda-ks.cfg
-rw-------root/root 953 2016-08-24 01:33-root/ Anaconda-ks.cfg

Example 3: append files to the archive (tar) file

The-r option is used to append files to an existing tar file. Next, add/etc/fstab to the Data.tar.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-RVF Data.tar/etc/fstab

Note: An append file operation cannot be made in a compressed tar file.

Example 4: release files and directories from the tar file

The-X option frees files and directories in the tar file. Let's release the contents of the tar file created above.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-XVF Myarchive.tar

This command releases the contents of the Myarchive.tar file in the current directory.

Example 5: Release tar file to specified directory

If you want to release the contents of the tar file to the specified folder or directory, use the-C option followed by the path of the specified file.

Copy Code code as follows:
[Root@linuxtechi ~]# tar-xvf myarchive.tar-c/tmp/

Example 6: release the specified file or directory in the tar file

Suppose you just release the ANACONDA-KS.CFG to/tmp directory in the tar file.

The syntax is as follows:

 # TAR–XVF {tar-file} {file-to-be-extracted}-c {path-where-to-extract}
[Root@linuxtechi tmp]# Tar-xvf/root/myarch Ive.tar root/anaconda-ks.cfg-c/tmp/
root/anaconda-ks.cfg [Root@linuxtechi-tmp]# ls-l/tmp/root/
Anaconda-ks.cfg
-rw-------. 1 root 953 Aug 01:33/tmp/root/anaconda-ks.cfg
[Root@linuxtechi tmp]# 

Example 7: Create and Compress archive files (. tar.gz or. tgz)

Suppose we need to package the/etc and/opt folders and compress them with the gzip tool. You can use the-Z option in the tar command. This tar file can have an extension of. tar.gz or. tgz.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-ZCPVF myarchive.tar.gz/etc//opt/

Or

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-ZCPVF myarchive.tgz/etc//opt/

Example 8: Create and Compress archive files (. tar.bz2 or. tbz2)

Suppose we need to package the/etc and/opt folders and use BZIP2 compression. You can use the-J option in the tar command. This tar file can have an extension of. tar.bz2 or. tbz.

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-JCPVF myarchive.tar.bz2/etc//opt/

Or

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-JCPVF myarchive.tbz2/etc//opt/

Example 9: Create a tar file after excluding the specified file or type

Use the –exclude option in the tar command when creating a tar file to exclude the specified file or type. Suppose you want to exclude the. html file when you create a compressed tar file.

Copy Code code as follows:
[Root@linuxtechi ~]# tar-zcpvf myarchive.tgz/etc//opt/--exclude=*.html

Example Ten: List the contents of the. tar.gz or. tgz file

Use the-t option to view the contents of the. tar.gz or. tgz file. As follows:

 [Root@linuxtechi ~]# TAR-TVF myarchive.tgz | more ... ..... ................. ...... ... drwxr-xr-x root/root 0 2016-09-07 08:41 etc/-rw-r--r--root/root 541 2016-08-24 01:23 etc/fstab-rw-------R Oot/root 0 2016-08-24 01:23 etc/crypttab lrwxrwxrwx root/root 0 2016-08-24 01:23 etc/mtab->/proc/self/mounts-rw- r--r--root/root 149 2016-09-07 08:41 etc/resolv.conf drwxr-xr-x root/root 0 2016-09-06 03:55 etc/pki/drwxr-xr-x root/ Root 0 2016-09-06 03:15 etc/pki/rpm-gpg/-rw-r--r--root/root 1690 2015-12-09 04:59 etc/pki/rpm-gpg/rpm-gpg-key-centos- 7-rw-r--r--root/root 1004 2015-12-09 04:59 etc/pki/rpm-gpg/rpm-gpg-key-centos-debug-7-rw-r--r--root/root 1690 2015-1 2-09 04:59 etc/pki/rpm-gpg/rpm-gpg-key-centos-testing-7-rw-r--r--root/root 3140 2015-09-15 06:53 etc/pki/rpm-gpg/ 
Rpm-gpg-key-foreman ........ ........................................ 

Examples: List the contents of the. tar.bz2 or. tbz2 file

Use the-t option to view the contents of the. tar.bz2 or. tbz2 file. As follows:

 [Root@linuxtechi ~]# TAR-TVF myarchive.tbz2 |
More ... ...... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... rwxr-xr-x root/root 0 2016-08-24 01:25 etc/pki/java/ lrwxrwxrwx root/root 0 2016-08-24 01:25 etc/pki/java/cacerts->/etc/pki/ca-trust/extracted/java/cacerts drwxr-xr-x Root/root 0 2016-09-06 02:54 etc/pki/nssdb/-rw-r--r--root/root 65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db-rw-r--r --Root/root 9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db-rw-r--r--root/root 16384 2010-01-12 16:21 Etc/pki/nssdb/key3 . db-rw-r--r--root/root 11264 2016-09-06 02:54 etc/pki/nssdb/key4.db-rw-r--r--root/root 451 2015-10-21 09:42 nssdb/pkcs11.txt-rw-r--r--root/root 16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db drwxr-xr-x root/root 0 2016-08-24 01:26 etc/pki/ca/drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/ca/certs/drwxr-xr-x root/root 0 2015-06-29 08:48 et C/pki/ca/crl/drwxr-xr-x root/root 0 2015-06-29 08:48 etc/pki/ca/newcerts/drwx------root/root 0 2015-06-29 08:48 etc/pki/ca/private/drwx------root/root 0 2015-11-20 06:34 etc/pki/rsyslog/drwxr-xr-x root/root 0 20 


 16-09-06 03:44 etc/pki/pulp/......... ...... ............................ ................. ........

Example: extract. tar.gz or. tgz files

Use the-X and-Z options to extract the. tar.gz or. tgz file. As follows:

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-ZXPVF myarchive.tgz-c/tmp/

The above command extracts the tar file to the/tmp directory.

Note: Today's tar command automatically checks the file's compression type before performing the decompression, which means that we can use the tar command to not specify the file's compression type. As follows:

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-XPVF myarchive.tgz-c/tmp/

Example: extract. tar.bz2 or. tbz2 files

Use the-j and-X options to extract the. tar.bz2 or. tbz2 file. As follows:

Copy Code code as follows:
[Root@linuxtechi ~]# TAR-JXPVF myarchive.tbz2-c/tmp/

Or

Copy Code code as follows:
[Root@linuxtechi ~]# tar xpvf myarchive.tbz2-c/tmp/

Example: scheduled backups using the TAR command
There are always some real time scenarios that require us to package the files and directories that we specify, and we have achieved the purpose of daily backups. Suppose you need to back up the entire/opt directory daily, you can create a cron task with the tar command to complete. As follows:

Copy Code code as follows:
[Root@linuxtechi ~]# tar-zcvf optbackup-$ (date +%y-%m-%d). tgz/opt/

Create a cron task for the above command.

Example: Create a compressed archive using-T and-X

Imagine a scenario in which files and directories that you want to archive and compress are recorded to a file, and then used as an incoming parameter to the TAR command to complete the archive, and sometimes you need to eliminate the specific paths recorded in the file mentioned above to archive and compress them.

Use the-t option in the TAR command to specify the input file, and use the-X option to specify the list of files that contains the file you want to exclude.

Suppose you want to archive/etc,/OPT,/home directories, and exclude/etc/sysconfig/kdump and/etc/sysconfig/foreman files, you can create/root/tar-include and/root/ Tar-exclude then enter the following separately:

 [Root@linuxtechi ~]# cat/root/tar-include
/etc
/opt
/home
[Root@linuxtechi ~]#
[ Root@linuxtechi ~]# cat/root/tar-exclude
/etc/sysconfig/kdump
/etc/sysconfig/foreman
[ Root@linuxtechi ~]# 

Run the following command to create a compressed archive file.

Copy Code code as follows:
[Root@linuxtechi ~]# tar zcpvf mybackup-$ (date +%y-%m-%d). Tgz-t/root/tar-include-x

Example: view the size of. Tar,. tgz and. tbz2 Files

Use the following command to view (compress) The volume of the tar file.

 [Root@linuxtechi ~]# Tar-czf-data.tar | Wc-c
427
[Root@linuxtechi ~]# tar-czf-mybackup-2016-09-09.tgz | wc-c
37956009
[Root@linuxtechi ~]# t ar-czf-myarchive.tbz2 | Wc-c
30835317
[Root@linuxtechi ~]# 

Example: split large volumes of tar files for multiple small files

The split command is used in Unix-like systems to split large volume files into small volume files. Large volumes of tar can, of course, be segmented using this command.

Suppose you need to split "mybackup-2016-09-09.tgz" into a small file of 6 MB per copy.
syntax:split-b <Size-in-MB> <tar-file-name>.<extension> "Prefix-name"

Copy Code code as follows:
[Root@linuxtechi ~]# split-b 6M mybackup-2016-09-09.tgz mybackup-parts

The above command will split the mybackup-2016-09-09.tgz file into several 6 MB small files in the current directory, with the file name MYBACKUP-PARTSAA ~ Mybackup-partsag. You can use the-D option in the split command above if you want to distinguish between a number and not a letter after you split the file.

 [Root@linuxtechi ~]# ls-l mybackup-parts*
-rw-r--r--. 1 root 6291456 Sep 03:05 MYBACKUP-PARTSAA-rw-r--r-
-. 1 root 6291456 Sep 03:05 mybackup-partsab
-rw-r--r--. 1 root root 6291456 Sep 03:05 Mybackup-partsac-
R W-r--r--. 1 root 6291456 Sep 03:05 mybackup-partsad
-rw-r--r--. 1 root root 6291456 Sep 03:05 mybackup-partsae-
R W-r--r--. 1 root 6291456 Sep 03:05 mybackup-partsaf
-rw-r--r--. 1 root root 637219 Sep 03:05 mybackup-partsag
[ro Ot@linuxtechi ~]# 


The split files are then transferred to other servers over the network, which can be merged into a separate tar file, as follows:

[Root@linuxtechi ~]# cat mybackup-partsa* > Mybackup-2016-09-09.tgz
[Root@linuxtechi ~]# 


I hope you like these different examples of the tar command.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.