The tutorial explains how to use the tar command in Linux. Learn the most frequently used tar command's options (c,v,f,t, and x) in detail through examples.
The tar command is used to create and manage archives in Linux. An archive is a collection of multiple files, which can be transferred as a single file or stored as a backup copy. An archive file created with the tar command is also known as a tarball file.
The tar command syntax
To create an archive file, the tar command uses the following syntax.
tar [options] [destination-file-name.tar] [source-files or directories]
To list or extract and archived file, the tar command uses the following syntax.
tar [options] [archived-file.tar]
The tar command options
The following table lists the most common tar command's options with their description.
Short option | Long option | Description |
-A | --catenate | Append the archive to the end of another archive. |
-c | --create | Create a new archive. |
-d | --diff, --compare | Find differences between archive and file system. |
--delete | Delete from the archive. | |
-r | --append | Append files to the end of an archive. |
-t | --list | List the contents of an archive. |
--test-label | Test the archive volume label and exit. | |
-u | --update | Append files which are newer than the corresponding copy in the archive. |
-x | --extract, --get | Extract files from an archive. |
--show-defaults | Show built-in defaults for various tar options and exit. | |
-? | --help | Display a short option summary and exit. |
--usage | Display a list of available options and exit. | |
--version | Print program version and copyright information and exit. | |
--check-device | Check device numbers when creating incremental archives. | |
-g | --listed-incremental=FILE | Handle new GNU-format incremental backups. |
-G | --incremental | Handle old GNU-format incremental backups. |
--ignore-failed-read | Do not exit with nonzero on unreadable files. | |
--no-check-device | Do not check device numbers when creating incremental archives. | |
-S | --sparse | Handle sparse files efficiently. |
-k | --keep-old-files | Don't replace existing files when extracting. |
--keep-newer-files | Don't replace existing files that are newer than their archive copies. | |
--keep-directory-symlink | Don't replace existing symbolic links to directories when extracting. | |
--no-overwrite-dir | Preserve metadata of existing directories. | |
--overwrite | Overwrite existing files when extracting. | |
--recursive-unlink | Recursively remove all files in the directory prior to extracting it. | |
--remove-files | Remove files from disk after adding them to the archive. | |
--skip-old-files | Don't replace existing files when extracting, silently skip over them. | |
-U | --unlink-first | Remove each file prior to extracting over it. |
-W | --verify | Verify the archive after writing it. |
-O | --to-stdout | Extract files to standard output. |
Short options are not available for all tasks. If an equivalent short option is not available for a specific task, use the long option. While specifying the options, you can use a dash (-) before the options or not. For example, you can specify options as -cvf or as cvf. Tar supports both styles.
Tar command c, v, f, t, and x options
The c, v, f, t, and x are the most frequently used options with the tar command. Let's understand these options in detail.
The option c creates an archive or a tarball from specified files or directories. If a directory is specified, all its files and sub-directories will be included in the archived file.
The option v displays the progress of archiving process at the command prompt. This option is optional. You can use it; if you want see what the tar command is doing. Or you can skip it; if you don't want to print the progress of archiving process at the command prompt.
The option f specifies the file where you want to store the output. If we don't specify this option, the tar command uses the standard output device (console or command prompt) to store the output data. In other words, if we don't specify the output file with this option, the tar command simply dumps all output data to the command prompt.
While specifying a file to store the output data, usually the extension .tar is used with file name. Although it's not required, still you should always use this extension with the file name. Using an extension for archived files helps keeping them separate from other files.
The option t lists the contents of the archive or tarball. Since this option lists the content of a file archive, the archive file must be specified with the option f.
The option x extracts the archive. Just like the option t, this option also needs an archive file specified with the option f.
The order of options is important. First specify the action (create, list or extract), next specify any additional requirement such verbose message, and in the last specify the name of file or device where the desired action should to be performed.
The following image shows the correct sequence of using these options.
The following syntaxes show the correct order of using these options.
tar -cvf [destination-file-name.tar] [source-files or directories] tar -tvf archive-file.tar tar -xvf archive-file.tar
Let's take some examples to understand how these options are used.
Creating an archive or tarball
To create an archive, three options c, v and f are used together. The following command creates an archive from a single file named test and puts that archive file in the same directory.
#tar -cvf test.tar test
The following image shows the above command.
To add multiple files into the archive, specify them as arguments. For example, the following command creates an archive file files.tar from files; a, b, c, d and f.
#tar -cvf files.tar a b c d f
The following image shows the output of the above command.
Just like the files, we can also specify the directories as arguments. If we specify a directory as an argument, all of its files and sub-directories are recursively added into the archived file. For example, the following command creates an archived file data.tar from the directory testdir.
#tar -cvf data.tar testdir
The following image shows the output of the above command.
If files and directories that you want to add into the archive file are not available in the current directory, you must have to specify their full paths. For example, the following command creates an archive conf.tar from the files: /etc/hosts, /etc/hostname and /etc/host.conf.
#tar -cvf /etc/hosts /etc/hostname /etc/host.conf
The following image shows the output of the above command.
Just like we can create an archive from files which are not available in the current directory, we can also put the archived file on any location or device. For example, the following command adds all files and directories from the home partition into the archive home.tar and stores the archive on the /tmp partition.
#tar -cvf /tmp/home.tar /home
The following image shows the output of the above command.
That's all for this tutorial. In the next parts of this tutorial, we will understand how to extract and manage an archived file. We will also take some advance examples of the tar command.
This tutorial is the first part of the article "How to use Tar Command in Linux Explained with examples". The next parts of this article are the following.
This is the second part of this article. It explains how to manage an archive after creation.
Tar command Compress and Extract Archives
This is the third and last part of this article. It explains how to compress and extract a compressed archive in detail.
If you like this tutorial, please don't forget to share it with friends through your favorite social network.