Tar command options and syntax explained

This tutorial introduces the tar command and explains the meaning and usages of its commonly used options, such as c, v, f and x, in detail through examples.

What is the tar?

The tar is a Linux command. It allows you to create and manage archives. 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

The tar command uses the following syntax to create an archive file.

tar [options] [destination-file-name.tar] [source-files or directories]

It uses the following syntax to list or extract an archived file.

tar [options] [archived-file.tar]

The tar command options

The following table lists commonly used options and their meanings.

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 that 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 Do not replace existing files when extracting.
--keep-newer-files Do not replace existing files that are newer than their archive copies.
--keep-directory-symlink Do not 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 before extracting it.
--remove-files Remove files from disk after adding them to the archive.
--skip-old-files Do not replace existing files when extracting.
-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. Use the long option if an equivalent short option is unavailable for a specific task. While specifying the options, you can use a dash (-) before the options. For example, you can specify options such as -cvf or 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.

The option c creates an archive or a tarball from specified files or directories. If you provide a directory, it includes all files from the specified directory and its sub-directories to the archive.

The option v displays the progress of the archiving process at the command prompt. This option is optional. Use it only to see the live progress of the archiving process.

The option f specifies the file where you want to store the output. If you do not provide this option, the tar command uses the standard output device (console or command prompt) to store the output data. If you do not specify the output file, it dumps all output data to the command prompt. The tar command is flexible about file names. It does not restrict you from using fancy file names. You can use any name with or without a file extension. Administrators generally use the .tar extension with the file name. It allows them to identify and manage archive files easily.

The option t lists the contents of the archive or tarball. To use this option, specify an archive file using the f option.

The option x extracts the archive. It also needs an archive file specified using the f option.

The order of options is essential. Specify options in the following order.

  • Specify the action (create, list or extract).
  • Specify additional requirements, such as a verbose message.
  • Specify the file name or device where the tar will perform the desired action.

The following image shows the correct sequence of using these options.

the sequence of tar 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

The tar command examples

The following are some basic usages of the tar command.

  • Creating an archive or tarball
  • Adding multiple files to an archive
  • Adding a partition to archive

This tutorial is part of the tutorial "The tar Command and its Options (c,v, f) Explained with Examples.". Other parts of this tutorial are as follows:

Chapter 1   Tar command options and syntax explained
Chapter 2   Tar command examples in Linux
Chapter 3  Tar command Compress and Extract Archives

Creating an archive or tarball

Use options c, v, and f to create an archive. The following command creates an archive from the test file and puts the archived file in the same directory.

#tar -cvf  test.tar test

creating tar archive

To add multiple files to the archive, specify them as arguments. For example, the following command creates the archive file files.tar and adds files a, b, c, d and f.

#tar -cvf files.tar a b c d f

adding multiple files to the archive

Like files, you can also specify the directories as arguments. If you provide a directory as an argument, it recursively adds all its files and sub-directories to the archived file. For example, the following command creates an archived data.tar from the testdir directory.

#tar -cvf data.tar testdir

adding files to archive

If the files and directories you want to add to the archive file are unavailable in the current directory, you must 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

adding a partition to archive

Adding a partition to archive

To add a partition to an existing archive or create a new archive of a partition, specify its mount point as an argument. You can also customize the output location. To save the archived file on a custom location, specify its full path as the argument. For example, the following command adds all files and directories from the home partition to the archive home.tar and stores the archived file on the /tmp partition.

#tar -cvf /tmp/home.tar /home

adding files from another directory

Conclusion

The tar command is one of the most widely used commands for creating, managing, and extracting archive files. Its versatility in combining multiple files into a single tarball simplifies file management, backup, and transfer processes. This tutorial introduced this command and its most commonly used options, such as -c (create), -v (verbose), -f (file), -t (list), and -x (extract).

ComputerNetworkingNotes Linux Tutorials Tar command options and syntax explained

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com