Tar Command in Linux with Examples
The tar command (short for Tape Archive) is one of the most widely used utilities in Linux for archiving multiple files and directories into a single file (an archive). It’s commonly used for creating backups, consolidating files for distribution, and compressing data to save space.
Tar preserves the directory structure and file metadata (permissions, timestamps, etc.) in the archive, making it ideal for system backups and file packaging.
In this article, we’ll explore the basics of tar along with practical examples to help you master its usage.
Basic Syntax of tar
tar [OPTION]... -f <archive_name.tar> <file_or_directory>...
Where the archive name usually has a .tar extension. The operation flags (like -c for create or -x for extract) and other options come before the -f (file name) flag.
Commonly Used tar Options
- -c – Create a new archive.
- -x – Extract files from an archive.
- -t – List the contents of an archive.
- -f – Specify the filename of the archive (this option must be followed by the archive name).
- -v – Verbose output; show files being processed.
- -z – Compress the archive using gzip (producing a .tar.gz file).
- -j – Compress the archive using bzip2 (producing a .tar.bz2 file).
- -J – Compress the archive using xz (producing a .tar.xz file).
- -C – Change to a specified directory before performing the operation (useful when extracting archives to a target location).
- –exclude – Exclude files or directories that match a pattern from being added to the archive.
Examples of tar in Action
1. Creating a tar Archive
This example creates a plain tar archive (uncompressed) from a directory:
tar -cvf archive.tar /path/to/directory/
Here, -c initiates archive creation, -v enables verbose output (listing files as they are archived), and -f specifies the archive filename (archive.tar). This command will bundle the contents of /path/to/directory/ into a single file named archive.tar.
2. Extracting a tar Archive
You can extract the contents of a tar archive into the current directory:
tar -xvf archive.tar
In this command, -x tells tar to extract files from the archive. The files from archive.tar will be unpacked into the current working directory (preserving the original directory structure). If you want to extract into a specific directory, use the -C option. For example, to extract into /tmp/extract_here, you could run:
tar -xvf archive.tar -C /tmp/extract_here
3. Creating a Compressed (gzip) tar Archive
Often you’ll want to compress the archive to save space. This example creates a gzip-compressed tar archive (a .tar.gz file):
tar -czvf archive.tar.gz /path/to/directory/
The -z flag tells tar to compress the archive using gzip. The result is an archive named archive.tar.gz. The flags -c, -v, and -f work as before, creating an archive from /path/to/directory/ with verbose output.
4. Extracting a Compressed Archive
Extracting a compressed tar archive (like .tar.gz or .tgz) is similar to extracting a plain tar, but include the -z option so tar knows to decompress:
tar -xzvf archive.tar.gz
This will unpack the contents of archive.tar.gz to the current directory. If the archive was compressed with bzip2 (.tar.bz2), you would use -j instead of -z (e.g., tar -xjvf archive.tar.bz2), and for xz-compressed archives (.tar.xz), use -J (e.g., tar -xJvf archive.tar.xz). The rest of the options (-xvf) function the same as in the previous extraction example.
5. Listing Contents of an Archive
Sometimes you may want to see what’s inside an archive without extracting it. Use the -t option to list the archive’s contents:
tar -tvf archive.tar
This will display a list of files and directories stored in archive.tar (with -v providing details like permissions and sizes). You can also list contents of compressed archives by adding the appropriate compression flag (for example, use tar -tzvf archive.tar.gz to list a gzip-compressed archive).
6. Excluding Files and Directories
To create an archive while excluding certain files or sub-directories, use the –exclude option. For example, suppose you want to archive a directory but skip all files ending in “.log”:
tar --exclude='*.log' -cvf logs_archive.tar /path/to/logs/
In this command, tar will archive everything under /path/to/logs/ into logs_archive.tar, except files that match the *.log pattern. You can use multiple –exclude options to omit several patterns or directories as needed.
7. Backing Up a Directory with tar
Tar is frequently used for backups. You can combine tar with a timestamp to keep versioned backups. For example, to back up the /etc directory and include the current date in the archive name:
tar -czvf /backup/etc-$(date +%F).tar.gz /etc
This command creates a gzip-compressed archive of the /etc directory and saves it as /backup/etc-YYYY-MM-DD.tar.gz (with the filename containing today’s date). Each time you run this, it produces a new backup file (e.g., etc-2025-09-11.tar.gz) without overwriting the previous backups. The combination of -c, -z, -v, and -f options creates a compressed archive, as explained above.
Complimentary commands
split
– If your tar archive is very large, the split command can divide it into smaller chunks (files) for easier storage or transfer. These chunks can later be reassembled (for example, using cat or tar itself can read split files sequentially).md5sum/sha256sum
– After creating an archive, use these tools to generate a checksum of the file. This is useful for verifying data integrity later; you can compare the checksum at backup time and restore time to ensure the archive hasn’t been corrupted.scp
– Once you’ve created a tar archive (especially for backup purposes), you may want to store it on a remote server. The scp command (secure copy) allows you to securely transfer files (like your new archive) to another host over the network.
Conclusion
The tar command is a versatile archiving tool that every Linux user should know. By mastering a few key options, you can efficiently combine files into archives, compress them to save space, and extract or inspect them when needed. Whether you’re packaging files for distribution or creating system backups, tar provides a robust solution while preserving file structure and permissions. For more advanced usage and options, refer to the tar manual by running man tar on your system. With practice, you’ll find tar invaluable for managing files and archives on Linux.
Related Commands:
zip
: Compress files into a ZIP archive (alternative archiving tool commonly used for cross-platform compatibility).gzip
: Compress or decompress individual files (often used in conjunction with tar for creating .tar.gz archives).bzip2
: Compress files using the Bzip2 algorithm (produces .bz2 files; tar can use this via the -j option).7zip
: Create or extract archives in 7z format (a high-compression archive format, via the 7z command on Linux).