The tar command bundles multiple files and directories into a single archive. It’s similar to zip, with one key difference: zip files are always compressed, while tar files can be compressed but don’t have to be. The most common form you’ll see—.tar.gz—is a tar archive compressed with gzip.
Table of Contents
Basic syntax
Create a compressed archive:
tar -czvf archivename.tar.gz filename...
-c— create a new archive.-z— compress with gzip.-v— verbose; list files as they’re processed.-f archivename.tar.gz— name of the archive file (must come last, right before the file list).filename...— a space-separated list of files and directories to include (e.g.,filename1 filename2).
Extract a compressed archive:
tar -xzvf archivename.tar.gz
Here -x means extract. Modern GNU tar can auto-detect the compression, so -z is often optional on extraction—but including it does no harm.
Create a tar archive
Create example.tar.gz containing filename1 and filename2:
tar -czvf example.tar.gz filename1 filename2
With -v, tar lists each item as it’s added:
filename1
filename2
On success tar otherwise prints nothing, so you can confirm the archive exists with ls:
ls
Output:
example.tar.gz filename1 filename2
To place the archive inside a specific directory, give its full path:
tar -czvf /home/user/example.tar.gz filename1 filename2
Extract a tar archive
Extract into the current directory:
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
tar -xzvf archive.tar.gz
Output:
file1.txt
file2.txt
docs/
docs/readme.md
Extract into a specific directory with -C:
tar -xzvf archive.tar.gz -C /home
The -C switch tells tar to change to /home before extracting, so the archive contents land there instead of in the current directory.
View the contents without extracting
Sometimes you just want to see what’s inside. Use -t (list) instead of -x:
tar -tvf logs_archive.tar.gz
Output:
-rw-r--r-- user/group 1024 2026-08-20 10:15 app.log
-rw-r--r-- user/group 2048 2026-08-21 09:02 error.log
-rw-r--r-- user/group 512 2026-08-22 14:33 access.log
-t lists the contents, -f names the archive, and -v shows the detailed (long) listing with permissions, size, and timestamps.
Search inside compressed files
You can often search archived logs without fully unpacking them.
Individual gzip-compressed files: zgrep
zgrep is the perfect tool for a single gzip-compressed file—for example, a rotated log like error.log.gz:
zgrep -Hna 'connection timeout' error.log.gz
Output:
error.log.gz:42:ERROR: connection timeout
The flags:
-H— print the file name that contains the match.-n— print the line number of the match.-a— treat the file as text.
Searching inside a .tar.gz archive
A .tar.gz is many files packed into one stream, so running zgrep on it searches the raw archive rather than each member cleanly. To search the actual file contents inside the archive, stream them through tar to grep:
tar -xzOf logs_archive.tar.gz | grep -na 'connection timeout'
-Owrites each extracted file to standard output instead of to disk.
Output:
42:ERROR: connection timeout
If you also need to know which archived file matched, extract just the log you care about to stdout:
tar -xzOf logs_archive.tar.gz error.log | grep -na 'connection timeout'
Choosing a compression format
tar supports several compressors—just swap the compression flag and the extension:
| Format | Create flag | Command |
|---|---|---|
gzip (.tar.gz) | -z | tar -czvf archive.tar.gz files... |
bzip2 (.tar.bz2) | -j | tar -cjvf archive.tar.bz2 files... |
xz (.tar.xz) | -J | tar -cJvf archive.tar.xz files... |
none (.tar) | (omit) | tar -cvf archive.tar files... |
gzip is fastest and most compatible; xz compresses the smallest but is slower. For extraction, modern tar -xvf archive.tar.* detects the format automatically.
Quick reference
| Task | Command |
|---|---|
| Create a gzip archive | tar -czvf archive.tar.gz files... |
| Extract an archive | tar -xzvf archive.tar.gz |
| Extract to a directory | tar -xzvf archive.tar.gz -C /path |
| List contents | tar -tvf archive.tar.gz |
Search a .gz log | zgrep -Hna 'text' file.gz |
Search inside a .tar.gz | tar -xzOf archive.tar.gz | grep 'text' |


