Tar Quickstart, create and extract files from archives.

The tar utility is used to work with archive files and has 8 modes of operation and several options. The most used operations are ...

Official documentation

GNU tar manual - GNU Project - Free Software Foundation

TLDR

The tar utility is used to work with archive files and has 8 modes of operation and several options, the utility has a special (non-standard) syntax order which DOES NOT CONFORM TO NORMAL command line usage, pay extra attention to your command arguments (see operations vs options below)! The most used operations are "--create or -c", "--list or -t" and "--extract or -x". The command has a long form, short form and old form syntax, all three styles/syntaxes may be intermixed in a single tar command. One operation must be specified and only one operation can be used at a time. Several or no options can be used for each operation. Some options only make sense together with one type of (contextual) operation. The options "--file or -f", "--verbose or -v" and "--compress or -Z" are the most frequently used options and work for all operational modes.

Examples

tar [operation] [option] [file.tar] [input]/[output]

Extracting archives/"untarring"/"unzipping tar files" -x

tar -xvf [tar-file.tar] [output]
tar -xvf album.tar #extracts the entire archive 
tar -xvf album.tar photos #extracts the folder or file "photos" in album.tar
tar xvf album.tar photos #old style flags

Listing files in archives -t

tar -tvf [tar-file.tar] 
tar tvf [tar-file.tar] #old style
tar -tvf [tar-file.tar] subfolder

Creating archives/"tarring"/"zipping tar files" -c

tar [tar-file.tar] [input] ... [input]
tar -cvf album.tar photo1 photo2 photo3 #tarring individual files or folders
tar cvf album.tar photo1 photo2 photo3 #old style flags
tar -cvf album.tar photos/ pictures/ #tarring folders

Documentation highlights

Core Functionality/Operations

  • Create
  • List
  • Extract
  • append
  • compare
  • concatenate
  • delete
  • update

Frequently used options

  • File
  • Verbose
  • Compress

Important stuff

  • tar can direct its output to available devices, files, or other programs (using pipes).
  • Archives created by tar are capable of preserving file information and directory structure.
  • Conventionally, tar archives are given names ending with ā€˜.tarā€™. Ā This convention can be broken and is not necessary for tar to operate.
  • Whenever you use ā€˜createā€™, tar will erase the current contents of the file named by ā€˜--file=archive-nameā€™ (ā€˜-f archive-nameā€™) if it exists.
  • TARs first argument must be an option
  • The order of the options becomes more important when you begin to use short forms. Ā 

Definitions

  • An archive is a single file which contains the contents of many files, while still identifying the names of the files, their owner(s), and so forth.
  • The files inside an archive are called members.
  • The term extraction refers to the process of copying an archive member (or multiple members) into a file in the file system.

Definition - Operations vs Options TAR.

  • Arguments to tar fall into one of two classes: operations, and options. Operations; exactly one of these is both allowed and required for any instance of using tar; you may not specify more than one. Options; You are not required to specify any options, and you are allowed to specify more than one at a time (depending on the way you are using tar at that time).

Gotchas

  • The order of the options becomes more important when you begin to use short forms. Ā 
  • List the arguments in the order that makes the commands easiest to understand.
 $ tar -cfv collection.tar blues folk jazz

In this case, tar will make an archive file called ā€˜vā€™, containing the files ā€˜bluesā€™, ā€˜folkā€™, and ā€˜jazzā€™, because the ā€˜vā€™ is the closest ā€œfile nameā€ to the ā€˜-fā€™ option, and is thus taken to be the chosen archive file name. Ā tar will try to add a file called ā€˜collection.tarā€™ to the ā€˜vā€™ archive file; if the file ā€˜collection.tarā€™ did not already exist, tar will report an error indicating that this file does not exist. Ā If the file ā€˜collection.tarā€™ does already exist (e.g., from a previous command you may have run), then tar will add this file to the archive. Because the ā€˜-vā€™ option did not get registered, tar will not run under ā€˜verboseā€™ mode, and will not report its progress.