Use tar to combine multiple files into an archive file

On this page:


Overview

In Unix and Unix-like operating systems (such as Linux), you can use the tar command (short for "tape archiving") to combine multiple files into a single archive file for easy storage and/or distribution. Additionally, you can use tar in conjunction with a compression utility, such as gzip or compress, to create a compressed archive file.

Create an archive file

To combine multiple files into a single archive file (for example, my_files.tar), use the following command (replace file1 and file2 with the names of the files you want to combine):

tar -cvf my_files.tar file1 file2

To combine all the files in a directory into a single archive file (for example, my_files.tar), use the following command (replace /path/to/my/directory with the absolute path to the directory containing the files you want to combine):

tar -cvf my_files.tar /path/to/my/directory
Note:
  • You can use any name in place of my_files.tar, but you should keep the .tar extension.
  • If you don't use the -f option, tar will assume you want to create a tape archive instead of combining a number of files.
  • The -v option tells tar to be verbose (report all files as they are added).

Create a compressed archive file

Many Linux distributions use GNU tar, a version of tar produced by the Free Software Foundation. If your system uses GNU tar, you can use tar in conjunction with the gzip file compression utility to combine multiple files into a compressed archive file.

For example:

  • To use tar and gzip to combine multiple files into a compressed archive file (for example, my_files.tar.gz), use the following command (replace file1 and file2 with the names of the files you want to combine):
    tar -cvzf my_files.tar.gz file1 file2
    
  • To use tar and gzip to combine all the files in a directory into a compressed archive file (for example, my_files.tar.gz), use the following command (replace /path/to/my/directory with the absolute path to the directory containing the files you want to combine):
    tar -cvzf my_files.tar.gz /path/to/my/directory
    
Note:
  • In the above examples, the -z option tells tar to use gzip to compress the archive as it is created.
  • The file extensions .tgz and .tar.gz are equivalent; both signify a tar archive file compressed with gzip.

If your system does not use GNU tar, but nonetheless has gzip, you can create a compressed tar archive file (for example my_files.tar.gz with the following command (replace file1 and file2 with the names of the files you want to combine):

tar -cvf - file1 file2 | gzip > my_files.tar.gz

If gzip isn't available on your system, you can use the compress utility to create a compressed archive (for example, my_files.tar.Z); for example (replace file1 and file2 with the names of the files you want to combine):

tar -cvf - file1 file2 | compress > my_files.tar.Z

Extract the contents of an archive file

To extract the contents of a tar archive file created by tar (for example, my_files.tar), use the following command:

tar -xvf my_files.tar

To extract the contents of a tar archive file compressed with gzip (for example, my_files.tar.gz), use the following command:

tar -xvzf my_files.tar.gz

If you are not using GNU tar and need to extract the contents of a tar archive file compressed with gzip (for example, my_files.tar.gz), use the following command:

gunzip -c my_files.tar.gz | tar -xvf -

To extract the contents of a tar archive file compressed with compress (for example, my_files.tar.Z), use the following command:

uncompress -c my_files.tar.Z | tar -xvf -

Additional information

When using the tar command, the order of the options sometimes matters. For example, some versions of tar (not GNU tar) require that the -f option be immediately followed by a space and the name of the tar archive file.

The tar command has many options available. For details, consult the tar manual page; on the command line, enter:

man tar

GNU tar comes with additional documentation, including a tutorial, accessible through the GNU Info interface. You can access this documentation by entering:

info tar

Within the Info interface, press ? (the question mark) for a list of commands.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document acfi in the Knowledge Base.
Last modified on 2023-09-29 15:24:10.