How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linux
- HolyHosting

- Oct 30
- 3 min read
How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linux
tar, short for tape archive, is a Linux command used to archive and compress files and folders. Learning how to use the tar command is essential for efficiently managing files in Linux.

What Is the tar Command?
The tar command lets you combine multiple files and directories into a single archive file, making data storage and transfer easier. The result of a tar command is typically a TAR file. When combined with gzip for additional compression, it becomes a .tar.gz file.
When working on Linux systems, such as a VPS (Virtual Private Server), the tar command offers several advantages:
Reduces the number of files you manage, helping organize storage more efficiently.
Minimizes file size and optimizes disk space when combined with tools like gzip or bzip2.
Creates complete directory backups for easy data preservation and recovery.
Simplifies file transfers between systems since you deal with a single file instead of many.
Supports incremental backup strategies by archiving only the changes since the last backup.
tar Command Options
The tar command includes many options for advanced compression and extraction. Here are the most essential ones:
-c: Create an archive file.
-x: Extract files from an archive.
-z: Add gzip compression when creating or extracting an archive.
-j: Add bzip2 compression when creating or extracting an archive.
-t: List the contents of an archive.
-u: Update files in an existing archive.
-r: Add files to an archive (similar to -u).
-A: Merge two archives into one.
Basic syntax:
tar [options] [archive-file] [file or folder to archive]
tar Command Examples in Linux
Before you proceed, make sure your VPS hosting is running properly.
Create a tar Archive File
Creating an archive for files and directories is simple. Example:
tar -cvf archive-file.tar file1 file2
-c: Create a new archive.
-v: Enable verbose mode for detailed output.
-f: Specify the name of the archive file.
To recursively archive a directory and its contents:
tar -cvf archive-file.tar directory1/
Create a .tar.gz File
To reduce file size and optimize storage, create a .tar.gz file using gzip compression:
tar -czvf archive-file.tar.gz file1 file2 directory1
The -z option adds gzip compression.
You can also create a .tgz file:
tar -czvf archive.tgz file1 file2 directory1
Create a .tar.bz2 File
The BZ2 format provides higher compression than gzip, but it’s slower to compress and extract.
tar -cjvf archive-file.tar.bz2 file1 file2 directory1
You can also use .tar.tbz or .tar.tb2 extensions:
tar -cjvf archive-file.tar.tbz file1 file2 directory1
tar -cjvf archive-file.tar.tb2 file1 file2 directory1
Extract .tar Files
To extract files in the current directory:
tar -xvf archive-file.tar
To extract files to a specific directory:
tar -xvf archive-file.tar -C /path/to/destination/
For .tar.gz files:
tar -xvzf archive-file.tar.gz
tar -xvzf archive-file.tar.gz -C /path/to/destination/
For .tar.bz2, .tar.tbz, or .tar.tb2 files:
tar -xjvf archive-file.tar.bz2
tar -xjvf archive-file.tar.tbz
tar -xjvf archive-file.tar.tb2
List the Contents of a tar Archive
tar -tvf archive-file.tar
For .tar.gz files:
tar -ztvf archive-file.tar.gz
For .tar.bz2 files:
tar -jtvf archive-file.tar.bz2
Extract a Single File from a tar Archive
tar -xvf archive-file.tar file1.sh
Or with a stripped path:
tar -xvf archive-file.tar --strip-components=1 file1.sh
For .tar.gz:
tar -xzvf archive-file.tar.gz file2.py
tar -xzvf archive-file.tar.gz --strip-components=1 file2.py
For .tar.bz2:
tar -xjvf archive-file.tar.bz2 file3.cpp
tar -xjvf archive-file.tar.bz2 --strip-components=1 file3.cpp
Extract Multiple Files from a tar Archive
tar -xvf archive-file.tar file1 file2 directory1
For .tar.gz:
tar -xzvf archive-file.tar.gz file3 file4 directory2
For .tar.bz2:
tar -xjvf archive-file.tar.bz2 file5 file6 directory3
Extract Files Matching a Pattern
tar -xvf archive-file.tar '*.txt'
For .tar.gz:
tar -xzvf archive-file.tar.gz '*.txt'
For .tar.bz2:
tar -xjvf archive-file.tar.bz2 '*.txt'
Add Files to an Existing tar Archive
tar -rvf archive-file.tar newfile1 newfile2
Or add directories:
tar -rvf archive-file.tar newdir1 newdir2
Note: You can’t add files to .tar.gz or .tar.bz2 archives directly.
Verify a tar Archive
tar -tvf archive-file.tar > /dev/null
This discards the output, performing a clean verification.(Not applicable to .tar.gz or .tar.bz2 files.)
Check the Size of an Archive
tar -czvf - archive-file.tar | wc -c
tar -czvf - archive-file.tar.gz | wc -c
tar -cjvf - archive-file.tar.bz2 | wc -c
Create Backups Using tar with Pipes
You can combine tar with other commands for more advanced tasks — for example, backing up and compressing a directory:
tar -czvf - project | gzip > project_backup.tar.gz
Conclusion
We hope this guide has been helpful! 🚀 Remember, don’t hesitate to ask questions on the HolyHosting Discord or contact our support team.
Follow us on Twitter @HolyHosting to stay up to date.

Stuffy @ HolyHosting


















