Tar Command in Linux: Create and Extract Archives with Examples

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:
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:
tar -cf archive.tar file1 file2 file3Here is the equivalent command using long-form options:
tar --create --file=archive.tar file1 file2 file3You 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:
tar -cf backup.tar /home/userUse the -v option to see the files that are being processed:
tar -cvf backup.tar /home/userCreating 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:
tar -czf archive.tar.gz file1 file2Creating 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:
tar -cjf archive.tar.bz2 file1 file2Creating 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:
tar -cJf archive.tar.xz file1 file2Creating 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:
tar --zstd -cf archive.tar.zst file1 file2Excluding 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:
tar -czf project.tar.gz --exclude='.git' --exclude='node_modules' project/You can also use wildcard patterns. The following command excludes all .log files:
tar -czf backup.tar.gz --exclude='*.log' /var/wwwExtracting Tar Archives
To extract a tar archive, use the --extract (-x) option followed by the archive name:
tar -xf archive.tarIt is common to add the -v option to print the names of the files being extracted:
tar -xvf archive.tarExtracting 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:
tar -xf archive.tar.gztar -xf archive.tar.bz2tar -xf archive.tar.xztar -xf archive.tar.zstExtracting 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:
tar -xf archive.tar -C /opt/filesThe 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:
tar -xf project-1.2.3.tar.gz --strip-components=1 -C /opt/projectThis 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:
tar -xf archive.tar file1 file2When 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:
tar -xf archive.tar dir1 dir2If you try to extract a file that does not exist in the archive, an error message is displayed:
tar -xf archive.tar READMEtar: README: Not found in archive
tar: Exiting with failure status due to previous errorsExtracting 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:
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:
tar -xpf backup.tar -C /restore/pathWhen 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:
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:
tar -tf archive.tarfile1
file2
file3To get more information such as the file owner
, file size, and timestamp
, add the --verbose (-v) option:
tar -tvf archive.tar-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 file3Listing Files in Compressed Archives
The -t option works with compressed archives as well. GNU tar detects the compression format automatically:
tar -tf archive.tar.gzFiltering the File List
You can pipe the output of tar -tf to grep
to search for specific files:
tar -tf archive.tar.gz | grep '\.conf$'To list only files in a specific directory within the archive:
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:
tar -tf archive.tar file1If 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:
tar -rvf archive.tar newfileNote 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:
tar --delete -f archive.tar file1Like --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:
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 .
| Command | Description |
|---|---|
tar -cf archive.tar files | Create an archive |
tar -czf archive.tar.gz files | Create a gzip-compressed archive |
tar -cjf archive.tar.bz2 files | Create a bzip2-compressed archive |
tar -cJf archive.tar.xz files | Create an xz-compressed archive |
tar --zstd -cf archive.tar.zst files | Create a zstd-compressed archive |
tar -xf archive.tar | Extract an archive |
tar -xf archive.tar -C /path | Extract to a specific directory |
tar -xf archive.tar file1 | Extract a specific file |
tar -xf archive.tar --wildcards '*.js' | Extract files matching a pattern |
tar -tf archive.tar | List archive contents |
tar -tvf archive.tar | List with details (owner, size, date) |
tar -rvf archive.tar newfile | Append a file to an archive |
tar --delete -f archive.tar file1 | Remove 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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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