17 Practical examples of TAR commands in Linux

Source: Internet
Author: User
Tags bz2 gpg






Tar (Tape ARchive, tape archive abbreviation, LCTT: Originally designed to package files on tape, now most of us use it to make a backup of 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 file, and the archive can be further compressed using technologies such as gzip or bzip2. In other words, the tar command can also be used for backup: First archive multiple files and directories into a single tar file or archive file, and then release the files and directories in the tar file when needed.












This article describes 17 practical examples of tar.






The tar command syntax is as follows:




< files >




Some common options in the TAR command are listed below:




--delete: From the archive file ( Instead of tape) remove -r,--append: Append files to the archive -t,--list: Lists the content contained in the archive Span class= "hljs-deletion" >--test-label: Test archive Volume label and exit -u,--update: Append updated files to archive file -x,--extract,--get: Release files and directories in the archive -c,--directory=dir: Change working directory to Dir-f,--file=archive: Specify (to be created or existing) archive file name -j for archive files,--xz: Use XZ compression for archive -p,--preserv E-permissions: Keep access to the original file -v,--verbose: Show command entire execution process -z, gzip : Use gzip compression for archived 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 file



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




[[Email protected] ~] # TAR-CVF Myarchive.tar/etc/root/anaconda-ks.cfg




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






Where the-coption indicates that you want to create a tar file, the-voption to export the tar to the screen, and the-foption to specify the archive file name.




Myarchive. TarSep 7myarchivetar[[email protected] ~]#    


Example 2: Listing content in an archive file



Use the option in the TAR command to–tquickly list what is contained in a file without releasing the files in it.




[[Email protected] ~] # TAR-TVF Myarchive.tar




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




[[Email protected] ~]   -08:root/anaconda-ks.cfg[[email protected] ~]#    


Example 3: Append file to archive (TAR) file



-roption to append files to an existing tar file. Next, add/etc/fstab to Data.tar.




[[Email protected] ~] # TAR-RVF Data.tar/etc/fstab




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




Example 4: Releasing files and directories from a tar file



-xoption to release the files and directories in the tar file. Below to release the contents of the tar file created above.




[[Email protected] ~] # TAR-XVF Myarchive.tar




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




Example 5: Releasing the tar file to the specified directory



If you want to release the contents of the tar file to the specified folder or directory, use the following option to add the path to the-Cspecified file.




[[Email protected] ~] # TAR-XVF Myarchive.tar-c/tmp/


Example 6: Releasing the specified file or directory in a tar file



Suppose you just release the anaconda-ks.cfg in the tar file to the/tmp directory.






The syntax is as follows:




# TAR–XVF {tar-file} {file-to-be-extracted}-C {path-where-to-extract}[[email protected] tmp]# TAR-XVF/ROOT/MYARC Hive.tar root/anaconda-ks.cfg-c/tmp/root/anaconda-ks.cfg[[email protected] tmp]:33/tmp/root/ Anaconda-ks.cfg[[email protected] tmp]#  


Example 7: Create and compress an archive file (. tar.gz or. tgz)



Suppose we need to pack the/etc and/opt folders and compress them with the gzip tool. You can use options in the TAR command-zto implement. This tar file can be extended to. tar.gz or. tgz.




[[Email protected] ~] # TAR-ZCPVF myarchive.tar.gz/etc//opt/




Or




[[Email protected] ~] # TAR-ZCPVF myarchive.tgz/etc//opt/


Example 8: Create and compress an archive file (. tar.bz2 or. tbz2)



Suppose we need to pack the/etc and/opt folders and use BZIP2 compression. You can use options in the TAR command-jto implement. This tar file can be extended to. tar.bz2 or. tbz.




[[Email protected] ~] # TAR-JCPVF myarchive.tar.bz2/etc//opt/




Or




[[Email protected] ~] # TAR-JCPVF myarchive.tbz2/etc//opt/


Example 9: Create a tar file after you exclude a specified file or type



Use the option in the TAR command–excludeto exclude the specified file or type when creating a tar file. Assume that you want to exclude. html files when you create a compressed tar file.




[[Email protected] ~] # TAR-ZCPVF myarchive.tgz/etc//opt/--exclude=*.html


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



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




[Email protected] ~]# 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-------root/root 0 2016-08-24 01:23 etc/crypttablrwxrwxrwx 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.confdrwxr-xr-x root/root 0 20 16-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/R Pm-gpg-key-centos-debug-7-rw-r--r--root/root 1690 2015-12-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 ..........................................................

Example 11: List the contents of a. tar.bz2 or. tbz2 file



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




[[Email protected] ~]# TAR-TVF MYARCHIVE.TBZ2 | More........................................................rwxr-xr-x Root/root 0 2016-08-24 01:25 etc/pki/java/lrwxrwxrwx root/root02016-08-2401:Etc/pki/java/cacerts-/etc/pki/ca-trust/extracted/java/cacertsdrwxr-xr-x Root/root 0 2016-09-06 02:54 etc/pki/nssdb/-rw-r--r--root/root655362010-01-1215:etc/pki/nssdb/cert8.db-rw-r--r--Root/root92162016-09-0602:etc/pki/nssdb/cert9.db-rw-r--r--Root/root163842010-01-1216:etc/pki/nssdb/key3.db-rw-r--r--Root/root112642016-09-0602:etc/pki/nssdb/key4.db-rw-r--r--Root/root4512015-10-2109:etc/pki/nssdb/pkcs11.txt-rw-r--r--Root/root16384 -01-12 :etc/pki/nssdb/secmod.dbdrwxr-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 etc/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 2016-09-06 03:44 etc/pki/pulp/... ..... ... .... ..... .... ..... ..... ........., ...., ... and .... .......-........      

Example 12: Unzip the. tar.gz or. tgz file



Use-xthe and-zoptions to decompress the. tar.gz or. tgz file. As follows:




[[Email protected] ~] # TAR-ZXPVF Myarchive.tgz-c/tmp/




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






Note: The current tar command automatically checks the file's compression type before performing the decompression action, which means that we can use the tar command as a compression type without specifying the file. As follows:




[Roo[email protected] ~] # TAR-XPVF Myarchive.tgz-c/tmp/


Example 13: Unzip the. tar.bz2 or. tbz2 file



Use-jthe and-xoptions to decompress the. tar.bz2 or. tbz2 file. As follows:




[[Email protected] ~] # TAR-JXPVF Myarchive.tbz2-c/tmp/




Or




[[Email protected] ~] # tar xpvf myarchive.tbz2-c/tmp/


Example 14: Scheduled backups using the TAR command



There are always some real-time scenarios that require us to package the specified files and directories for the purpose of daily backups. Assuming you need to back up the entire/opt directory every day, you can create a cron task with the tar command to complete. as follows:




[[Email protected] ~] # TAR-ZCVF optbackup-$ (date +%y-%m-%d). tgz/opt/




Create a cron task for the above command.




Example 15: Creating a compressed archive with-T and-X



Imagine a scenario where files and directories that you want to archive and compress are recorded to a file, and then the file is used as an incoming parameter to the TAR command to complete the archive task, and sometimes it is necessary to exclude the specific path documented in the above file from being archived and compressed.






Use the option in the TAR command-Tto specify the input file, using the-Xoption to specify a list of files to exclude.






Suppose you want to archive the/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:




[[Email protected] ~] # Cat/root/tar-include/etc/opt/home[[email protected] ~#[[email protected] ~]# CAT/ROOT/TAR-EXCLUDE/ETC /sysconfig/kdump/etc/sysconfig/foreman[[email protected] ~]#  




Run the following command to create a compressed archive file.




[[Email protected] ~] # tar ZCPVF mybackup-$ (date +%y-%m-%d). tgz-t/root/tar-include-x/root/tar-exclude


Example 16: View the size of the. tar,. tgz, and. tbz2 files



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




[[Email protected] ~] # Tar-czf-data.tar | wc-c427[[email protected] ~]# tar-czf-mybackup-2016-09-09.tgz | wc-c37956009[ [email protected] ~]# tar-czf-myarchive.tbz2 | wc-c30835317[[email protected] ~]#


Example 17: Splitting a bulky tar file into 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 also be used to split the command.






Suppose you need to split "mybackup-2016-09-09.tgz" into small files of 6 MB per copy.




Syntax: split-b <size-in-mb> <tar-file-name>.<extension> "prefix- name "      


[[Email protected] ~] # split-b 6M mybackup-2016-09-09.tgz mybackup-parts




The above command will split the mybackup-2016-09-09.tgz file into multiple 6 MB small files in the current directory, with the file name MYBACKUP-PARTSAA ~ Mybackup-partsag. If you want to differentiate a file by a number instead of a letter, you can use the options in the split command above-d.




[Email protected] ~]#Ls-Lmybackup-parts*-rw-r--r--. 1RootRoot 6291456SEP 10 03: 05Mybackup-partsaa-rw-r--r--. 1RootRoot 6291456SEP 10 03: 05Mybackup-partsab-rw-r--r--. 1RootRoot 6291456SEP 10 03: 05Mybackup-partsac-rw-r--r--. 1RootRoot 6291456SEP 10 03: 05Mybackup-partsad-rw-r--r--. 1 root root 6291456 sep 10 03 :05 mybackup-partsae -rw-r--r--. 1 root root 6291456 sep 10 03 :05 mybackup-partsaf -rw-r--r--. 1 root root 637219 sep 10 03 :05 mybackup-partsag [[email protected] ~]#        



These split files are then transferred to other servers over the network and can be merged into a single tar file, as follows:




[[Email protected] ~] # cat mybackup-partsa* > Mybackup-2016-09-09.tgz[[email protected] ~]#  




I hope you like these several different examples of the tar command. Feel free to comment and share your experience.






17 Practical examples of TAR commands in Linux




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.