The tar command in Linux is used to bundle up multiple files and/or directories.
It’s similar to the zip command. However, zip files are compressed by definition; tar files can be compressed, but don’t have to be.
Table of Contents
How tar
Works in Linux
The tar
command in Linux stands for tape archive. It is used for creating, extracting, and manipulating archive files (single files containing multiple files or directories). Though it was originally used for backing up data to tape drives, it is now commonly used for backups, file compression, and transfer in Linux systems.
Basic Tar Syntax
tar [options] archive_name files_to_archive
archive_name
: Name of the archive to be created or extracted.files_to_archive
: The files or directories you want to include in the archive.
Common tar
Options
-c
: Create a new archive.-x
: Extract the contents of an archive.-v
: Verbose mode (shows the files being processed).-f
: Specify the name of the archive file.-z
: Compress the archive using gzip.-j
: Compress the archive using bzip2.-J
: Compress the archive using xz.-C
: Change to a specified directory before performing the operation.
Basic Tar Commands
1. Creating a Tar Archive
To create a .tar
archive:
Get Your Linux Course!
Join our Linux Course and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux today!tar -cvf archive_name.tar /path/to/directory_or_file
For example:
tar -cvf wordpress_backup.tar /var/www/wordpress
2. Creating a Compressed Archive with gzip
To create a .tar.gz
file:
tar -czvf wordpress_backup.tar.gz /var/www/wordpress
3. Extracting a Tar Archive
To extract an archive:
tar -xvf archive_name.tar
For example:
tar -xvf wordpress_backup.tar
4. Extracting a Compressed Archive
For .tar.gz
:
tar -xzvf wordpress_backup.tar.gz
For .tar.bz2
:
tar -xjvf wordpress_backup.tar.bz2
For .tar.xz
:
tar -xJvf wordpress_backup.tar.xz
5. Listing the Contents of an Archive
To list the contents without extracting:
tar -tvf archive_name.tar
6. Excluding Files from an Archive
To exclude certain files while creating an archive:
tar -czvf archive_name.tar.gz /path/to/directory --exclude='*.log'
How Tar Works Behind the Scenes
- Creating an Archive:
tar
combines files or directories into a single file with their metadata (e.g., file names, permissions, sizes). If compression is used (-z
,-j
, or-J
), the archive is compressed. - Extracting an Archive:
tar
reads the metadata and restores files. With compression, it decompresses the archive first.
Common Use Cases of tar
- Backup: Creating backups of directories and files.
- Transfer: Bundling files for easier transfer.
- Software Distribution: Packaging software as
.tar.gz
or.tar.bz2
. - Archives: Storing historical data like logs.
Example Commands Recap
- Create an uncompressed tar archive:
tar -cvf archive_name.tar /path/to/directory
- Create a compressed tar archive with gzip:
tar -czvf archive_name.tar.gz /path/to/directory
- Extract a tar archive:
tar -xvf archive_name.tar
- Extract a compressed tar.gz archive:
tar -xzvf archive_name.tar.gz
- List contents of a tar archive:
tar -tvf archive_name.tar
- Exclude files when creating a tar archive:
tar -czvf archive_name.tar.gz /path/to/directory --exclude='*.log'
Conclusion
The tar
command is a versatile tool for creating, extracting, and managing archive files in Linux. It’s widely used for backups, file transfers, and distributing software. Understanding how tar
works will make managing files in Linux more efficient.