GNU tar creates, lists, extracts, and updates archive files, with options for common compression formats such as gzip and xz. The command patterns below cover everyday tasks; replace example filenames and paths with your own, and inspect an archive before extracting it.
What GNU tar does
The name tar comes from “tape archive,” but GNU tar is used just as often with files on local disks. It bundles files and directory trees into an archive, normally with the .tar extension. Compression is a separate operation: GNU tar can invoke a compression program while creating or reading an archive, producing names such as .tar.gz or .tar.xz.
The basic command shape is:
tar [options] archive-name files-or-directories
GNU tar accepts options in both traditional short form and long form. Short options are often grouped, as in -czf; long options such as --create and --gzip are easier to read. In a short option group, the archive filename follows -f, so put f last when using this compact style.
Quick command cheatsheet
- Create an uncompressed archive:
tar -cf backup.tar project/ - Create a gzip-compressed archive:
tar -czf backup.tar.gz project/ - Create an xz-compressed archive:
tar -cJf backup.tar.xz project/ - List archive contents:
tar -tf backup.tar - List contents with details:
tar -tvf backup.tar - Extract into the current directory:
tar -xf backup.tar - Extract gzip or xz archives:
tar -xzf backup.tar.gzortar -xJf backup.tar.xz - Extract into a chosen directory:
tar -xf backup.tar -C /path/to/destination - Extract one member:
tar -xf backup.tar project/README.md - Add a file to an uncompressed archive:
tar -rf backup.tar notes.txt - Check an archive for read errors:
tar -tf backup.tar > /dev/null
The main operation letters are -c (create), -x (extract), -t (list), and -r (append). The -f option identifies the archive file. Compression flags include -z for gzip, -j for bzip2, and -J for xz. GNU tar can often infer compression when reading, but specifying it makes commands clearer and is useful for portability.
Creating archives and choosing compression
To archive several paths, list them after the archive name: tar -czf site.tar.gz index.html assets/. Tar stores the paths you give it, so running the command from a suitable parent directory usually makes the archive easier to unpack. For example, from the directory containing project/, archiving project/ preserves that directory as the top-level member.
To control stored path names, change directory for the archive operation with -C:
tar -czf project.tar.gz -C /home/sam project
This stores project/..., rather than the full path /home/sam/project/.... In general, tar archives names relative to the chosen working directory; it does not normally store an absolute leading slash.
Compression choices involve a trade-off between time and archive size. Gzip is widely supported and usually quick. Xz can produce smaller files in many cases but may take more time and resources. To adjust gzip compression, use an option such as -z together with --gzip’s related compression-level option, for example tar -czf archive.tar.gz --gzip -6 folder/. Compression levels and exact performance depend on the data and system.
Listing, extracting, and selecting files
Before extracting an archive from an unfamiliar source, list it with tar -tf file.tar. This shows the member names without writing them to disk. Use -v for a more detailed listing, including file modes, ownership information, sizes, and timestamps where available.
Extracting with -C sets the destination directory; the directory should already exist. To extract only a named member, give its archived path after the archive argument. The name must match the path as stored in the archive, which may differ from the path you see on your computer. To select members by a pattern, GNU tar provides --wildcards; quote patterns so the shell does not expand them first:
tar -xf backup.tar --wildcards 'project/*.txt'
Tar options can also exclude paths while creating an archive: tar -czf source.tar.gz --exclude='*.log' source/. Exclusion patterns are interpreted by tar, and their matching behavior can depend on the pattern and path. Check the resulting listing when the exact contents matter.
Appending, updating, and common limitations
GNU tar’s -r option appends members to an uncompressed archive. It does not append to a compressed archive such as .tar.gz or .tar.xz, because compressed data cannot be extended in the same simple way. For a compressed archive, recreate it with the desired files instead.
The -u option updates an uncompressed archive with files that are newer than the archived versions; it is not a general-purpose synchronization tool. For routine backups, creating a fresh archive or using a dedicated backup tool may be easier to reason about. Also, adding a changed file to a tar archive does not remove an older member with the same name; listings can show duplicate names, and extraction may overwrite earlier copies.
GNU tar can preserve Unix metadata such as permissions and timestamps. Ownership restoration is separate: extracting as an ordinary user generally cannot assign files to arbitrary owners. Options such as --same-owner affect ownership restoration, while --numeric-owner uses numeric user and group IDs. Consider the implications before restoring archives across machines or accounts.
Safety and troubleshooting
Archives can contain unexpected paths, symlinks, or files that overwrite existing data. Listing an archive first and extracting into a new, empty directory reduces the chance of damaging unrelated files. Do not assume an archive from an unknown source is safe merely because its filename looks ordinary.
- “Not found in archive”: list members with
tar -tfand copy the exact archived path, including any top-level directory. - Wrong compression format: check the filename and try the matching flag, such as
-zfor gzip or-Jfor xz. A misleading extension does not change the file’s actual format. - Permission denied: choose a destination you can write to, or inspect ownership and permissions before deciding whether elevated privileges are appropriate.
- Archive appears to have extra directory levels: this usually reflects the paths included at creation time. Use
-Cwhen creating or extracting to control the base directory. - Corrupt or truncated archive:
tar -tfcan reveal read failures, but it cannot repair missing data. Obtain a clean copy or recreate the archive from the original files.
Frequently Asked Questions
What does tar -xzf mean?
It extracts (-x) an archive, reads it as gzip-compressed data (-z), and takes the archive filename from the following argument (-f). For example: tar -xzf files.tar.gz.
How do I create a tar.gz file?
Use tar -czf archive.tar.gz directory/. The command creates a tar archive and compresses it with gzip.
How can I see what is inside a tar file?
Run tar -tf archive.tar for member names, or tar -tvf archive.tar for a detailed listing. Listing does not extract the files.
Can tar extract a single file?
Yes. Specify its exact archived path after the archive, as in tar -xf archive.tar folder/readme.txt. Use tar -tf if you are unsure of that path.
Can I add files to a .tar.gz archive?
Not directly with tar’s append operation. Recreate the compressed archive with the old and new files, or append to an uncompressed .tar archive using tar -rf.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Multi-Asset Trading Platforms: Why They’re Essential
- Creality K3 4-Nozzle 3D Printer: 4.8s Swap?
- Notepad Remove Duplicate Lines: 3 Simple Ways
- API Security Checklist: A Backend Dev’s Guide
- Storefront Hero Images: Desktop vs. Mobile Versions
- Device Encryption vs BitLocker: Key Differences