How to Use the Tar Command in Linux
VPS Apr 28, 2022 Edward S. 4min Read
Tar is one of the most widely used Linux commands for compression. There are great benefits in using tar, that’s why it’s loved by the pros. Here’s all you need to get you started.
Tar stands for Tape archive and is used to compress a collection of files and folders.
Download Complete Linux Commands Cheat Sheet
- How to Use the Tar Command in Linux
- What Is Tar Command Used For
- How to Use Tar in Linux
- Creating a .tar Archive File in Linux
- Creating a .tar.gz File in Linux
- Creating a .tar.bz2 File in Linux
- How to Unzip .tar Files in Linux
- How to List the Contents of an Archive in Linux
- How to Unzip a Single .tar File
- How to Extract Multiple Files from .tar Archives
- Extract multiple files with a pattern
- How to Add Files to a .tar Archive
- How to Verify a .tar Archive in Linux
- How to Check Archive Size in Linux
- Conclusion
What Is Tar Command Used For
In most cases, once the compression is done using tar results in a .tar file. Further compression is done using gzip which would result in a .tar.gz file.
With tar, you can compress and decompress files. Tar comes with multiple options though there are few which you may need to remember.
The advantages of tar:
- Tar, when it comes to compression has a compression ratio of 50%, which means it compresses efficiently
- Drastically reduces the size of packaged files and folders
- Tar does not alter the features of files and directories. The permissions and other features remain intact while compressing
- Tar is widely available across most common Linux versions. This is available on Android firmware as well as supported older Linux flavors.
- Compresses and Decompresses fast
- Easy to use
While this helps us understand tar’s benefits, one question to answer is under what scenario would you choose to use it?
- If you are working on Linux based systems and require file compression
- To transfer a huge collection of files and folders from one server to another
- Take a backup of your website, data or anything else
- To reduce the usage of space on your system, since compression will occupy less space
- To upload and download folders
How to Use Tar in Linux
Let’s learn what basic operations you can perform by using tar. Before we start, you’ll need to SSH into your VPS. Here’s a guide to help you!
Creating a .tar Archive File in Linux
You can create .tar compressions for a file as well as directories. An example of such an archive is:
tar -cvf sampleArchive.tar /home/sampleArchive
Here /home/sampleArchive is the directory which needs to be compressed creating sampleArchive.tar.
The command uses –cvf options which stand for:
- c – This creates a new .tar file
- v – shows a verbose description of the compression progress
- f – file name
Creating a .tar.gz File in Linux
If you want better compression, then you can also use .tar.gz. An example of this is:
tar -cvzf sampleArchive.tar.gz /home/sampleArchive
The additional option z represents gzip compression. Alternatively, you can create a .tgz file which is similar to tar.gz. An example of this is as shown below:
tar -cvzf sampleArchive.tgz /home/sampleArchive
Creating a .tar.bz2 File in Linux
The .bz2 file provides more compression compared to gzip. However, this would take more time to compress and decompress. To create this, you need to use the -j option. An example of the operation is:
tar -cvjf sampleArchive.tar.bz2 /home/sampleArchive
This is similar to .tar.tbz or .tar.tb2. An example of this is as shown below:
tar -cvjf sampleArchive.tar.tbz /home/sampleArchive
tar -cvjf sampleArchive.tar.tb2 /home/sampleArchive
How to Unzip .tar Files in Linux
The tar command can also be used to extract a file. The below command will extract files in the current directory:
tar -xvf sampleArchive.tar
If you want to extract to a different directory then you can use the -C option. One example is as shown below:
tar -xvf sampleArchive.tar -C /home/ExtractedFiles/
A similar command can be used to uncompress .tar.gz files as shown below:
tar -xvf sampleArchive.tar.gz
tar -xvf sampleArchive.tar.gz -C /home/ExtractedFiles/
.tar.bz2 or .tar.tbz or .tar.tb2 files can be uncompressed similarly. It would require the following command in the command line:
tar -xvf sampleArchive.tar.bz2
How to List the Contents of an Archive in Linux
Once the archive is built, you can list the contents by using a command similar to the one below:
tar -tvf sampleArchive.tar
This will display the complete list of files along with timestamps and permissions. Similarly, for .tar.gz, you can use a command like:
tar -tvf sampleArchive.tar.gz
This would also work for .tar.bz2 files as shown below:
tar -tvf sampleArchive.tar.bz2
How to Unzip a Single .tar File
Once an archive is created, you can extract a single file. One such example is as shown below:
tar -xvf sampleArchive.tar example.sh
Here example.sh is a single file which will be extracted from sampleArchive.tar. Alternatively, you can also use the following command:
tar --extract --file= sampleArchive.tar example.sh
To extract a single file from .tar.gz you can use a command similar to the one shown below:
tar -zxvf sampleArchive.tar.gz example.sh
Or alternatively:
tar --extract --file= sampleArchive.tar.gz example.sh
To extract a single file from .tar.bz2 you can use a command like this:
tar -jxvf sampleArchive.tar.bz2 example.sh
Or alternatively one like this:
tar --extract --file= sampleArchive.tar.bz2 example.sh
As you can see, the tar command has a lot of flexibility in its syntax.
How to Extract Multiple Files from .tar Archives
In case you want to extract multiple files, use the below format of the command:
tar -xvf sampleArchive.tar "file1" "file2"
For .tar.gz you can use:
tar -zxvf sampleArchive.tar.gz "file1" "file2"
For .tar.bz2 you can use:
tar -jxvf sampleArchive.tar.bz2 "file1" "file2"
Extract multiple files with a pattern
If you want to extract specific patterns of files like only .jpg from the archive, use wildcards. A sample of such command is as shown below:
tar -xvf sampleArchive.tar --wildcards '\*.jpg'
For .tar.gz you can use:
tar -zxvf sampleArchive.tar.gz --wildcards '\*.jpg'
For .tar.bz2 you can use:
tar -jxvf sampleArchive.tar.bz2 --wildcards '\*.jpg'
How to Add Files to a .tar Archive
While you can extract specific files, you can also add files into an existing archive. To do this, we would use the -r option which stands for append. Tar can add both files and directories.
Below is an example where we are adding example.jpg into the existing sampleArchive.tar.
tar -rvf sampleArchive.tar example.jpg
We can also add a directory. In the example below, the image_dir directory is added into sampleArchive.tar
tar -rvf sampleArchive.tar image\_dir
You cannot add files or folder to .tar.gz or .tar.bz2 files.
How to Verify a .tar Archive in Linux
Using tar you can verify an archive. This is one of the ways you can do it:
tar -tvf sampleArchive.tar
This cannot be applied on .tar.gz or .tar.bz2 files.
How to Check Archive Size in Linux
Once you create an archive you can check the size of it. This will be displayed in KB (Kilobytes).
Below are examples of such commands with different archive files:
tar -czf - sampleArchive.tar | wc -c
tar -czf - sampleArchive.tar.gz | wc -c
tar -czf - sampleArchive.tar.bz2 | wc -c
Conclusion
As you can see, tar is a truly powerful tool that every Linux enthusiast should know. You can further explore the manual pages for the tar command by executing the man tar command. We hope this article helped up your Linux game! See you in the next one.
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)