Sep 18, 2023
How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linux
tar, short for tape archive, is a Linux command for archiving and compressing files and folders. You must learn how to use the tar command to efficiently manage Linux files.
This article will guide you through practical applications of the Linux tar command. We’ll explore its various options and provide you with tar usage examples.
What Is the tar Command Used For?
The tar command allows you to bundle multiple files and directories into a single archive, making data storage and transfer effortless. Typically, the outcome of a tar command is a TAR file. If you use gzip for further compression, the result becomes a .tar.gz file.
When working on Linux systems, such as a virtual private server (VPS), the tar command offers several advantages:
- Reduces the number of managed files, helping you organize storage more efficiently.
- Minimizes file sizes and optimizes the disk space when combined with tools like gzip or bzip2 for different compression formats.
- Creates backups of entire directories to preserve and easily recover data.
- Simplifies file transfer between systems, as you deal with a single file instead of many.
- Helps with incremental backup strategies, archiving only the changes since the last backup.
tar Command Options
The tar command has various options for advanced file compression and decompression. Here are several essential tar options you can use:
- -c – Create an archive file.
- -x – Extracts files from an archive.
- -z – Adds gzip compression when creating or extracting an archive file.
- -j – Adds bzip2 compression when creating or extracting an archive file.
- -t – Lists the contents of an archive file.
- -u – Update files to an existing archive.
- -r – Append files to an archive, similar to the -u option.
- -A – Link two archives into a single archive.
The basic tar command syntax is as follows:
tar [options] [archive-file] [file or folder to be archived]
tar Command Examples in Linux
In this section, you’ll learn some basic tar command operations. But before we proceed, make sure your VPS Hosting is running properly.
Creating a tar Archive File in Linux
Creating a tar archive for files and directories is simple. Here’s an example of how to create an archive:
tar -cvf archive.tar file1 file2
This example employs the -cvf options:
- -c – creates a new archive.
- -v – activates verbose mode, providing detailed output throughout the archiving process.
- -f – specifies the name of the new archive file.
Replace archive.tar with your desired archive file name and file1 and file2 with the file names you want to include in the archive.
To perform recursive archiving of a directory and all its contents, use the following command:
tar -cvf archive.tar directory1/
Creating a .tar.gz File in Linux
To minimize file sizes and optimize storage, create .tar.gz archives using the tar command and gzip compression. Follow the command below to generate this archive file type:
tar -czvf archive.tar.gz file1 file2 directory1
The -z option signifies gzip compression.
Alternatively, create a TGZ file like a tar.gz archive. Here is the Linux tar example:
tar -czvf archive.tgz file1 file2 directory1
Creating a .tar.bz2 File in Linux
Use the BZ2 file format for better compression levels than gzip. However, this format takes longer to compress and decompress.
Here’s how to create a .tar.bz2 archive:
tar -cjvf archive.tar.bz2 file1 file2 directory1
Similarly, employ .tar.tbz or .tar.tb2 as alternatives:
tar -czvf archive.tar.tbz file1 file2 directory1
tar -czvf archive.tar.tb2 file1 file2 directory1
Unzipping .tar Files in Linux
The tar command can also be used for extracting files. To extract files in the current directory, enter the following command:
tar -xvf archive.tar
The -C option lets you extract files into a specific directory. Here’s an example:
tar -xvf archive.tar -C /path/to/destination/
Remember to replace /path/to/destination/ with the full path of your desired folder.
For .tar.gz files, employ analogous commands:
tar -xvf archive.tar.gz
tar -xvf archive.tar.gz -C /path/to/destination/
The extraction process for .tar.bz2, .tar.tbz, or .tar.tb2 files follows the same principles:
tar -xjvf archive.tar.bz2
tar -xjvf archive.tar.tbz
tar -xjvf archive.tar.tb2
Listing the Contents of an Archive File in Linux
Once you’ve made an archive, you can list all the contents by employing the following command:
tar -tvf archive.tar
It will display a comprehensive list of files, along with timestamps and permissions.
For .tar.gz archive files, use the following command format:
tar -ztvf archive.tar.gz
The command also extends to .tar.bz2 files:
tar -jtvf archive.tar.bz2
Unzipping a Single tar File
You can also extract one file from a compressed archive. Here’s an example of how to do it:
tar -xvf archive.tar file1.sh
In this instance, file1.sh is a single file within archive.tar that will be extracted.
Consider this command for an alternative:
tar -xvf archive.tar --strip-components=1 file1.sh
Extracting a file from a .tar.gz archive follows a similar pattern:
tar -xzvf archive.tar.gz file2.py
Here’s another approach:
tar -xzvf archive.tar.gz --strip-components=1 file2.py
For .tar.bz2 archives, use one of these tar commands:
tar -xjvf archive.tar.bz2 file3.cpp
tar -xjvf archive.tar.bz2 --strip-components=1 file3.cpp
Extracting Multiple Files from tar Archives
To extract multiple files from a tar archive, use the following command:
tar -xvf archive.tar file1 file2 directory1
The command above extracts file1, file2, and directory1 from archive.tar.
Adopt a similar approach to extract files from .tar.gz archives:
tar -xzvf archive.tar.gz file3 file4 directory2
Likewise, for .tar.bz2 archives:
tar -xjvf archive.tar.bz2 file5 file6 directory3
Extracting Multiple Files With a Pattern
You can use wildcards when extracting files with specific patterns. Here’s an example of how to execute this command:
tar -xvf archive.tar '*.txt'
The above command only extracts TXT files from archive.tar.
The command is almost the same for .tar.gz archive files:
tar -xzvf archive.tar.gz '*.txt'
Similarly, here’s how to extract specific files from .tar.bz2 archives:
tar -xjvf archive.tar.bz2 '*.txt'
Adding Files to a tar Archive
The tar command also lets you easily add files and directories to an existing archive file. To add files to a tar archive, enter the following command:
tar -rvf archive.tar newfile1 newfile2
The -r option lets you append files to an existing archive, while -v enables verbose mode for detailed output.
You can also add directories to a tar file using this command:
tar -rvf archive.tar newdir1 newdir2
However, you can’t add new files or folders to .tar.gz or .tar.bz2 files.
Verifying a tar Archive File in Linux
You can employ the tar command to verify compressed archives. Here’s a command example:
tar -tvf archive.tar > /dev/null
The > /dev/null section directs any output to be discarded, creating a clean verification process.
This verification method doesn’t apply to .tar.gz or .tar.bz2 files.
Checking the Archive Size in Linux
Checking the size of an archive file can be useful. The archive file size is typically displayed in kilobytes (KB).
Below, you’ll find tar examples to check the sizes of different compressed archive files:
tar -czvf - archive.tar | wc -c
tar -czvf - archive.tar.gz | wc -c
tar -cjvf - archive.tar.bz2 | wc -c
Using the tar Command With Pipes to Create Backups
You can combine the tar command with other commands to simplify complex tasks. One useful tar command technique is to back up and restore files using pipes.
For instance, to compress and back up a directory named project into a .tar.gz archive file, enter:
tar -czvf - project | gzip > project_backup.tar.gz
The gzip command compresses the output and directs it into the file project_backup.tar.gz.
The tar command remains a vital tool for compressing and archiving files in Linux distributions. Its straightforward yet powerful capabilities let you optimize storage space, simplify file transfers, and improve overall data organization.
By mastering practical applications of the tar command, you’ve gained an essential skill to enhance your Linux-based workflows.
tar Command FAQ
This section will answer the most common questions about the tar command.
Why Use the tar Command in Linux?
The Linux tar command is used to archive and compress files. It lets you consolidate multiple files and folders into a single archive file while preserving the directory structure. This command improves storage efficiency, simplifies data management, and facilitates seamless file transfer.
Can I Use the tar Command to Create Multi-Volume Archives?
Yes, you can use tar to split large compressed archives into multiple volumes. This is especially useful when dealing with storage limitations. By using the -M option, you can create multi-volume tar archives and ensure seamless data distribution across multiple media or systems.
Can I Use the tar Command With Other Linux Commands?
Absolutely. The tar command seamlessly integrates with other Linux commands. You can use pipes (|) to direct the tar output into other commands, like gzip for compression or scp for remote transfers. This flexibility empowers you to enhance your management of archive files, backups, and distribution tasks.
Can I Use the tar Command With Other Linux Commands?
Learn More Linux Commands for File Management
How to Remove Files and Directories
How to Create an Empty File
How to Locate a File
How to Change File Ownership with Chown Command
How to Unzip Files in Linux
How to Change File Permissions with Chmod Command
How to Rename a File
How to Check File Type
How to Create a Symbolic Link (Symlink)