The Linux tar command is a powerful utility for creating archives, commonly known as “tarballs,” that bundle multiple files or directories into a single file. Originally designed for tape backups, it remains a widely-used tool on Linux systems today, capable of archiving data with or without compression.
Below, you’ll find practical step-by-step instructions on how to tar a directory, including methods for creating both compressed and uncompressed archives, as well as extracting their contents.
Method 1: Creating a Simple Uncompressed Tar Archive
If you need to quickly bundle files or a directory without compression, you can create an uncompressed tar archive. Here’s how:
archive.tar:tar cf archive.tar directory_name
Replace directory_name with the name of the folder you want to archive. This command combines your files into one archive without compression, making the process quick and requiring minimal processing power.
tar xf archive.tar
This will extract the contents into your current directory.
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Method 2: Creating a Compressed Tar Archive with gzip
Gzip compression is one of the most common methods for compressing tar archives, producing files with a .tar.gz extension. Here’s how to create a gzip-compressed tar archive:
tar czf archive.tar.gz directory_name
This command uses the -z option to apply gzip compression, significantly reducing the archive size compared to an uncompressed tar archive.
tar xzf archive.tar.gz
To extract to a specific directory instead of your current location, add the -C option followed by the path:
tar xzf archive.tar.gz -C /path/to/destination
Method 3: Creating a Compressed Tar Archive with bzip2
Bzip2 compression typically provides a higher compression ratio than gzip, resulting in smaller archives, though it requires slightly more processing time. Here’s how you can create a bzip2-compressed archive:
tar cjf archive.tar.bz2 directory_name
Here, the -j option specifies bzip2 compression.
tar xjf archive.tar.bz2
Method 4: Creating a Compressed Tar Archive with xz
Xz compression provides even smaller file sizes than gzip or bzip2 but requires more time and CPU power. This method is ideal when prioritizing storage space savings over speed.
tar cJf archive.tar.xz directory_name
The -J option tells tar to use xz compression.
tar xJf archive.tar.xz
Additional Useful Options
Here are a few additional options that can be useful when working with tar archives:
Extract Specific Files: To extract only certain files from an archive, specify the file names after the archive name:
tar xzf archive.tar.gz file1 file2
Exclude Files or Directories: Use the --exclude option to omit specific files or directories:
tar czf archive.tar.gz directory_name --exclude=directory_name/exclude_this
Verbose Mode (-v): Add this to see detailed output of files being processed:
tar czvf archive.tar.gz directory_name
Using the tar command in Linux is straightforward once you understand the basic options. Whether you’re backing up files, transferring data, or saving space, tar archives offer a versatile and powerful solution for managing your files efficiently.






