Working with tar archives in Linux
The essential guide to creating, extracting, and managing tar files.
tar is the go-to tool in Linux for bundling files and directories into a single archive. Importantly, tar itself does not compress anything — it simply packs files together while preserving directory structure, permissions, ownership, timestamps, and other metadata. Compression comes from separate tools:
- gzip → .tar.gz or .tgz (most common)
- bzip2 → .tar.bz2 / .tbz (better compression, slower)
- xz → .tar.xz (excellent compression, modern standard)
That's why most Linux archives have double extensions like archive.tar.gz.
Core commands & flags
-c— create a new archive-x— extract files from an archive-t— list contents (without extracting)-f— specify the archive filename (almost always needed)-v— verbose: show files as they are processed-z— filter through gzip (for .tar.gz)-j— filter through bzip2 (for .tar.bz2)-J— filter through xz (for .tar.xz)-C— change to a directory before extracting (or for packing)--exclude— skip files or patterns
Creating archives
-
Standard .tar.gz (fast & widely compatible):
tar -czvf archive.tar.gz /path/to/folderCurrent directory:
tar -czvf archive.tar.gz . -
.tar.bz2 (stronger compression, slower):
tar -cjvf archive.tar.bz2 /path/to/folder -
.tar.xz (best compression ratio, modern choice):
tar -cJvf archive.tar.xz /path/to/folder -
Plain .tar (no compression — rare today):
tar -cvf archive.tar /path/to/folder
Our products and services
Extracting archives
-
tar.gz:
tar -xzvf archive.tar.gz -
tar.bz2:
tar -xjvf archive.tar.bz2 -
tar.xz:
tar -xJvf archive.tar.xz -
Extract to a specific folder (creates it if missing):
tar -xzvf archive.tar.gz -C /path/to/destination
Listing archive contents (without extracting)
tar -tzvf archive.tar.gz # .gz
tar -tjvf archive.tar.bz2 # .bz2
tar -tJvf archive.tar.xz # .xz
tar -tvf archive.tar # plain tar
Practical examples
- Exclude logs and cache folders:
tar -czvf archive.tar.gz /var/www --exclude='*.log' --exclude='cache'
- Append files to an existing tar archive:
tar -rvf archive.tar newfile.txt
- Extract only a specific file or folder:
tar -xzvf archive.tar.gz path/inside/archive/file.php
- Archive only PHP files found by search:
find /var/www -name "*.php" | tar -czvf php_files.tar.gz -T -
Pro tips
- Always use
-vwhen working manually — you can see exactly what’s being processed. - For very large archives, add progress feedback:
--checkpointи--checkpoint-action=dot(shows a dot every 1000 files) - Extract and cd into the folder in one line:
tar -xzvf archive.tar.gz && cd ${archive%.tar.gz} - Prefer .tar.xz today — it usually gives the smallest size with good speed on modern hardware.
Help
If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!