The tar command in Unix creates and reads archive files, which package files and directories together while preserving useful filesystem information. It can also work with compression tools, so a tar archive is often compressed into a smaller file for storage or transfer.
The name comes from “tape archive,” reflecting the command’s history of writing collections of files to sequential storage. Today, tar is commonly used for backups, software source packages, and moving directory trees, whether or not tape is involved.
What a tar archive contains
An archive is a single file that holds records for one or more files and directories. These records can include names, file data, permissions, ownership information, and timestamps. Tar is a packaging format and command-line tool; it does not inherently mean that the contents are compressed.
This distinction explains common file extensions. A file ending in .tar is generally an uncompressed tar archive. Extensions such as .tar.gz or .tgz indicate a tar archive compressed with gzip. Likewise, .tar.xz commonly indicates xz compression, and .tar.bz2 indicates bzip2 compression. The extension is a helpful convention, not proof that a file is valid or safe.
Tar is often compared with ZIP, but they are not identical tools or formats. Tar primarily bundles files; compression is typically supplied separately. ZIP commonly combines archiving and compression in one format. Which format is appropriate depends on the systems and applications that need to read the result.
How the command is structured
A typical command has the form tar options archive-name file-or-directory. Options tell tar whether to create, list, or extract an archive, and may specify compression or other handling. The archive name identifies the file to read or write; the remaining arguments identify the paths to include or extract.
Many examples use short options grouped after a hyphen. In traditional usage, some tar options are written without a leading hyphen, while common modern implementations accept both styles. Because behavior and available options can differ among Unix-like systems, consult the local tar manual page when a command needs to work across machines.
For example, tar -cf project.tar project creates an uncompressed archive from a directory named project. In this option group, c means create, f says that the next argument is the archive filename, and project.tar is that filename. The directory is the input. The order matters: the value for f must follow the option group.
Creating and compressing archives
To create a gzip-compressed archive, use tar -czf project.tar.gz project. Here, z requests gzip compression in addition to creating the archive and specifying its filename. With tar implementations that support it, tar -cJf project.tar.xz project requests xz compression. Another familiar option is j for bzip2, as in tar -cjf project.tar.bz2 project.
These examples package the directory itself, so the archive normally contains entries beginning with project/. To archive several selected paths, list them after the archive filename, as in tar -czf materials.tar.gz notes.txt images. Use names relative to the current working directory when practical; relative paths generally make archives easier to unpack in a chosen location.
Tar does not usually remove the original files after creating an archive. Creating a backup therefore leaves the source tree in place unless a separate command changes it. That is useful, but it also means a successful archive command is not by itself a complete backup strategy: the archive should be stored somewhere appropriate and checked for readability.
Listing archive contents before extracting
To see what an archive contains without unpacking it, use tar -tf project.tar. Add z for a gzip-compressed archive, as in tar -tzf project.tar.gz. The t option lists entries; f identifies the archive file. Listing is a useful first check when you did not create the archive or do not know its directory layout.
Look at the paths shown in the listing. An archive that contains a top-level directory is often easier to extract safely than one whose entries are scattered directly into the current directory. Check for unexpected absolute paths, parent-directory components such as ../, or names that could overwrite files you care about. Treat archives from unknown sources cautiously.
Extracting files and choosing a destination
To extract an uncompressed archive into the current directory, use tar -xf project.tar. For gzip compression, use tar -xzf project.tar.gz; for xz, use tar -xJf project.tar.xz where supported. The x option means extract. Tar creates the listed files and directories relative to the extraction location, subject to permissions and the archive’s contents.
To choose a destination, use -C followed by an existing directory, for example tar -xzf project.tar.gz -C /tmp/unpack. In common tar implementations, -C changes the working directory for the operation, so place it where the local manual documents and check the command if portability matters. The destination generally needs to exist and be writable.
Extracting into a new, empty directory is a sensible habit when the archive’s contents are unfamiliar. It helps prevent accidental mixing with existing files and makes it easier to inspect what was unpacked. Be particularly careful when running extraction with elevated privileges: an archive may contain entries that affect sensitive locations or permissions.
Useful options and practical workflows
Tar supports options beyond the basic create, list, and extract operations. Their exact availability and spelling can vary, but these common patterns cover many everyday tasks:
-vprints names as tar processes them. It can help show what is being added or extracted, though verbose output is not a substitute for checking the result.-C directorychanges the directory used for a portion of the operation, which can help control paths in an archive.--exclude=patterncan omit matching paths when creating an archive on implementations that support this long option. Check how patterns are interpreted before relying on it.-prequests preservation of permissions during extraction in implementations that provide this behavior. Ownership and permission restoration can depend on user privileges and system rules.
A practical backup workflow is to decide which directory should appear at the top of the archive, create the archive from a suitable parent directory, and then list its contents. For instance, moving to the parent directory and archiving project produces clear relative names. Archiving absolute paths instead can lead to warnings or surprising extraction behavior, so relative paths are usually preferable.
Common mistakes and troubleshooting
A frequent error is confusing the archive filename with the input path. In tar -czf backup.tar.gz documents, the archive being written is backup.tar.gz, and documents is what goes inside it. If the arguments are misplaced, tar may report an error or treat a different path as the archive.
Another common misunderstanding is expecting a plain .tar file to be compressed. It is only an archive unless a compression option or separate compression tool is used. Conversely, using the wrong compression option when reading an archive can produce an error. Check the file’s actual format when its extension cannot be trusted, and use the matching option or a format-detection feature if the local tar supports one.
“Cannot open” or “permission denied” errors often mean that the archive path is wrong, the current user cannot read or write the file, or the destination directory is unavailable. Check the current directory with pwd, inspect filenames with a directory-listing command, and verify access rights before changing permissions. Avoid using administrator privileges as a first response; they can make an incorrect extraction more damaging.
If extraction appears to create files in an unexpected location, inspect the archive listing and your current directory. If a command reports that a file changed while being read, the source may have been modified during archive creation; retry when the source is stable if a consistent snapshot matters. Tar archives are not inherently encrypted, so compression should not be mistaken for confidentiality.
Tar compared with other archiving choices
Choose tar when you want a widely used Unix-style way to bundle a directory tree, preserve file-related metadata, or combine files with a separate compression method. It is especially convenient in shell scripts because its basic operations are straightforward and can be combined with other command-line tools.
A ZIP archive may be more convenient when recipients expect a format that commonly packages and compresses files together or when they use graphical archive utilities. Other archive formats have their own strengths and compatibility requirements. Before sharing, consider the recipient’s operating system, needed metadata, compression support, and whether the archive must be encrypted. Tar alone does not provide encryption; use an appropriate, separately selected encryption method when confidentiality is required.
Frequently Asked Questions
Does tar compress files?
Not by itself in the usual sense. Tar bundles files into an archive; options such as -z or -J request compression with a separate compression method supported by the tar implementation.
How do I extract a tar.gz file?
Use tar -xzf archive.tar.gz to extract it into the current directory. To reduce the chance of overwriting existing files, first list the contents with tar -tzf archive.tar.gz and extract into a suitable empty directory.
How can I see what is inside a tar file?
Use tar -tf archive.tar for an uncompressed archive, or add the matching compression option, such as -z for gzip. Listing lets you inspect paths without extracting the files.
What do the letters in tar -czf mean?c creates an archive, z requests gzip compression, and f identifies the archive filename that follows. The following argument is the source file or directory to include.
Is a tar file the same as a ZIP file?
No. Tar is principally an archive format, and compression is commonly added separately. ZIP generally combines packaging and compression in its format. Compatibility and metadata needs can help determine which one to use.
In summary
Tar is a Unix command for creating, inspecting, and extracting archives. Remember the core options—c to create, t to list, x to extract, and f to name the archive—and add the appropriate compression option when needed. Inspect unfamiliar archives, choose extraction destinations carefully, and consult the system’s manual page for implementation-specific details.