Tar Command in Linux: Create and Extract Archives with Examples

By 

Updated on

10 min read

Linux Tar Command

When you need to back up a project, move a directory tree, or package files for distribution, tar is one of the most practical Linux tools to use.

The tar command creates tar files by converting a group of files into an archive. It also extracts tar archives, displays a list of the files included in the archive, adds files to an existing archive, and performs various other operations.

Tar was originally designed for creating archives to store files on magnetic tape, which is why it has its name “Tape ARchive”.

This guide explains how to use the tar command to create, extract, and list tar archives through practical examples and detailed explanations of the most common options.

Tar Command Syntax

There are two versions of tar, BSD tar and GNU tar , with some functional differences. Most Linux systems come with GNU tar pre-installed by default.

The general syntax for the tar command is as follows:

txt
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
  • OPERATION - Only one operation argument is allowed and required. The most frequently used operations are:
    • --create (-c) - Create a new tar archive.
    • --extract (-x) - Extract the entire archive or one or more files from an archive.
    • --list (-t) - Display a list of the files included in the archive.
  • OPTIONS - The most frequently used options are:
    • --verbose (-v) - Show the files being processed by the tar command.
    • --file=archive-name (-f archive-name) - Specifies the archive filename.
  • ARCHIVE_NAME - The name of the archive.
  • FILE_NAME(s) - A space-separated list of filenames to be extracted from the archive. If not provided, the entire archive is extracted.

When executing tar commands, you can use the long or the short form of operations and options. The long forms are more readable, while the short forms are faster to type. Long-form options are prefixed with a double dash (--), and short-form options are prefixed with a single dash (-), which can be omitted.

Creating Tar Archives

Tar supports a range of compression programs such as gzip, bzip2, xz, lzip, lzma, lzop, and compress. When creating compressed tar archives, it is an accepted convention to append the compressor suffix to the archive file name. For example, an archive compressed with gzip should be named archive.tar.gz.

To create a tar archive, use the -c option followed by -f and the name of the archive.

For example, to create an archive named archive.tar from the files file1, file2, and file3:

Terminal
tar -cf archive.tar file1 file2 file3

Here is the equivalent command using long-form options:

Terminal
tar --create --file=archive.tar file1 file2 file3

You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless the --no-recursion option is specified.

The following example creates an archive named backup.tar of the /home/user directory:

Terminal
tar -cf backup.tar /home/user

Use the -v option to see the files that are being processed:

Terminal
tar -cvf backup.tar /home/user

Creating Tar Gz Archives

Gzip is the most popular algorithm for compressing tar files. When compressing with gzip, the archive name should end with tar.gz or tgz.

The -z option tells tar to compress the archive using gzip as it is created. For example, to create a tar.gz archive from the given files:

Terminal
tar -czf archive.tar.gz file1 file2

Creating Tar Bz2 Archives

Another popular compression algorithm is bzip2. When using bzip2, the archive name should end with tar.bz2 or tbz.

To compress with bzip2, use the -j option:

Terminal
tar -cjf archive.tar.bz2 file1 file2

Creating Tar Xz Archives

Xz offers higher compression ratios than gzip or bzip2, making it popular for distributing source code and large packages. When using xz, the archive name should end with tar.xz or txz.

To compress with xz, use the -J option:

Terminal
tar -cJf archive.tar.xz file1 file2

Creating Tar Zst Archives

Zstandard (zstd) offers fast compression and decompression with good compression ratios. Many modern Linux distributions use .tar.zst for package and archive workflows.

To compress with zstd, use the --zstd option:

Terminal
tar --zstd -cf archive.tar.zst file1 file2

Excluding Files When Creating Archives

The --exclude option skips files or directories matching a pattern. For example, to archive a project directory while excluding .git and node_modules:

Terminal
tar -czf project.tar.gz --exclude='.git' --exclude='node_modules' project/

You can also use wildcard patterns. The following command excludes all .log files:

Terminal
tar -czf backup.tar.gz --exclude='*.log' /var/www

Extracting Tar Archives

To extract a tar archive, use the --extract (-x) option followed by the archive name:

Terminal
tar -xf archive.tar

It is common to add the -v option to print the names of the files being extracted:

Terminal
tar -xvf archive.tar

Extracting Compressed Archives

When extracting compressed archives such as tar.gz or tar.bz2 , you do not need to specify a decompression option. GNU tar automatically detects the compression format:

Terminal
tar -xf archive.tar.gz
Terminal
tar -xf archive.tar.bz2
Terminal
tar -xf archive.tar.xz
Terminal
tar -xf archive.tar.zst

Extracting to a Different Directory

By default, tar extracts the archive contents in the current working directory . Use the --directory (-C) option to extract to a specific directory:

Terminal
tar -xf archive.tar -C /opt/files

The target directory must already exist before running the command.

Stripping Leading Path Components

Source tarballs typically extract into a top-level directory like project-1.2.3/. Use --strip-components=N to drop the first N path components during extraction:

Terminal
tar -xf project-1.2.3.tar.gz --strip-components=1 -C /opt/project

This is the standard pattern when extracting source releases into an existing build directory.

Extracting Specific Files

Sometimes you need to extract only a few files from an archive. Append a space-separated list of file names after the archive name:

Terminal
tar -xf archive.tar file1 file2

When extracting files, you must provide their exact names including the path, as printed by --list (-t).

Extracting one or more directories from an archive works the same way:

Terminal
tar -xf archive.tar dir1 dir2

If you try to extract a file that does not exist in the archive, an error message is displayed:

Terminal
tar -xf archive.tar README
output
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

Extracting Files Using Wildcards

To extract files based on a wildcard pattern, use the --wildcards option and quote the pattern to prevent the shell from interpreting it:

Terminal
tar -xf archive.tar --wildcards '*.js'

Preserving Permissions and Ownership

By default, GNU tar applies your current umask when extracting files as a regular user. Use -p (--preserve-permissions) when you need to restore the stored mode bits exactly:

Terminal
tar -xpf backup.tar -C /restore/path

When extracting system backups as root, GNU tar also restores the original owner by default. Use --same-owner explicitly if you want the command to document that restore behavior:

Terminal
sudo tar --same-owner -xpf backup.tar -C /

Listing Tar Archive Contents

Use the --list (-t) option to view the contents of an archive without extracting it.

The following command lists the files in archive.tar:

Terminal
tar -tf archive.tar
output
file1
file2
file3

To get more information such as the file owner , file size, and timestamp , add the --verbose (-v) option:

Terminal
tar -tvf archive.tar
output
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file1
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file2
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file3

Listing Files in Compressed Archives

The -t option works with compressed archives as well. GNU tar detects the compression format automatically:

Terminal
tar -tf archive.tar.gz

Filtering the File List

You can pipe the output of tar -tf to grep to search for specific files:

Terminal
tar -tf archive.tar.gz | grep '\.conf$'

To list only files in a specific directory within the archive:

Terminal
tar -tf archive.tar.gz | grep '^etc/nginx/'

Listing Specific Files

To check whether a particular file exists in the archive, pass the filename after the archive name:

Terminal
tar -tf archive.tar file1

If the file exists, its name is printed. If it does not, tar displays an error.

Adding Files to an Existing Archive

To add files or directories to an existing tar archive, use the --append (-r) operation:

Terminal
tar -rvf archive.tar newfile

Note that you cannot append files to compressed archives (.tar.gz, .tar.bz2, .tar.xz). You must decompress the archive first, append the files, and then recompress.

Removing Files from an Archive

Use the --delete operation to remove files from an archive:

Terminal
tar --delete -f archive.tar file1

Like --append, the --delete operation only works on uncompressed tar archives.

Streaming Archives Over SSH

Tar can write to or read from standard input and output by passing - as the archive name. This makes it easy to copy a directory tree to a remote host without creating an intermediate file:

Terminal
tar -cf - /var/www | ssh user@remote 'tar -xf - -C /backup'

The local tar streams the archive to stdout, ssh forwards it, and the remote tar reads it from stdin. Add -z on both sides to compress in transit.

Quick Reference

For a printable quick reference, see the tar cheatsheet .

CommandDescription
tar -cf archive.tar filesCreate an archive
tar -czf archive.tar.gz filesCreate a gzip-compressed archive
tar -cjf archive.tar.bz2 filesCreate a bzip2-compressed archive
tar -cJf archive.tar.xz filesCreate an xz-compressed archive
tar --zstd -cf archive.tar.zst filesCreate a zstd-compressed archive
tar -xf archive.tarExtract an archive
tar -xf archive.tar -C /pathExtract to a specific directory
tar -xf archive.tar file1Extract a specific file
tar -xf archive.tar --wildcards '*.js'Extract files matching a pattern
tar -tf archive.tarList archive contents
tar -tvf archive.tarList with details (owner, size, date)
tar -rvf archive.tar newfileAppend a file to an archive
tar --delete -f archive.tar file1Remove a file from an archive
tar -czf archive.tar.gz --exclude='*.log' dir/Create archive excluding a pattern

Troubleshooting

tar: Cannot open: No such file or directory
The archive path is incorrect or you are running the command from a different directory. Verify the path with ls and use an absolute path if needed.

tar: file: Not found in archive
The file path inside the archive does not match what you requested. List archive contents first with tar -tf archive.tar and copy the exact path.

tar: Permission denied
You do not have enough permissions to read source files or write the destination. Run the command with a writable output path, or use sudo only when required.

Extraction to -C /path fails
The target directory does not exist. Create it first: mkdir -p /path, then run extraction again.

tar: Removing leading ‘/’ from member names
Informational, not an error. Tar strips leading slashes by default so archives extract relative to the current directory. Use -P (--absolute-names) only with trusted archives when you intentionally need absolute paths, because extraction can write outside the target directory.

FAQ

What is the difference between .tar, .tar.gz, and .tar.xz?
A .tar file is an uncompressed archive. A .tar.gz file is compressed with gzip, and .tar.xz is compressed with xz. Xz provides the highest compression ratio, gzip is the fastest, and bzip2 falls in between.

Do I need to specify -z or -j when extracting?
No. GNU tar automatically detects the compression format during extraction. You can use tar -xf for all archive types.

How do I check the contents of an archive without extracting it?
Use tar -tf archive.tar.gz to list files, or tar -tvf archive.tar.gz for detailed output including permissions, owner, and size.

Can I add files to a compressed archive?
No. The --append (-r) and --delete operations only work on uncompressed .tar archives. Decompress the archive first, modify it, then recompress.

How do I exclude multiple directories when creating an archive?
Use --exclude for each directory: tar -czf backup.tar.gz --exclude='.git' --exclude='node_modules' --exclude='*.log' project/

Conclusion

The tar command covers the daily archive tasks you are most likely to need: use -c to create, -x to extract, and -t to inspect archives before unpacking them. For deeper command coverage, keep the quick reference nearby when working with backups or source tarballs.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page