How to Compress and Extract Files Using the tar Command on Linux

Last Updated: Feb 10, 2024 by

If you’re a Linux user, you may have come across the term “tar” when dealing with files. But what exactly is tar, and how can you use it to compress and extract files on your Linux system?

In this article, we’ll explore the basics of file archiving and compression on Linux using the tar command. We’ll cover what tar is, how it works, and how you can use it to compress and extract files on your Linux system.

What is Tar?

Tar, short for “tape archive”, is a command-line utility used for archiving and compressing files on Linux systems. It was originally designed for use with magnetic tape storage, but is now commonly used for file archiving and compression on Linux.

Tar works by combining multiple files into a single archive file, which can then be compressed using various algorithms to reduce its size. This makes it easier to store and transfer large amounts of data.

How Does Tar Work?

Tar works by creating a single archive file that contains all the files and directories you want to compress. This archive file can then be compressed using various algorithms, such as gzip or bzip2, to reduce its size.

When extracting files from a tar archive, the archive file is first decompressed, and then the individual files and directories are extracted from it.

Compressing Files with Tar

To compress files using tar, you’ll first need to create an archive file. This can be done using the tar command, followed by the -c flag to create a new archive, and the -f flag to specify the name of the archive file.

For example, to create an archive file named “myfiles.tar”, you would use the following command:

tar -cf myfiles.tar file1.txt file2.txt directory1

This will create an archive file named “myfiles.tar” that contains the files “file1.txt” and “file2.txt”, as well as the directory “directory1”.

Compressing with gzip

Once you have created your archive file, you can compress it using the gzip algorithm by adding the -z flag to your command:

tar -czf myfiles.tar.gz file1.txt file2.txt directory1

This will create a compressed archive file named “myfiles.tar.gz” that contains the same files and directories as the original archive file.

Compressing with bzip2

Alternatively, you can use the bzip2 algorithm to compress your archive file by adding the -j flag to your command:

tar -cjf myfiles.tar.bz2 file1.txt file2.txt directory1

This will create a compressed archive file named “myfiles.tar.bz2” using the bzip2 algorithm.

Extracting Files with Tar

To extract files from a tar archive, you’ll need to use the tar command again, this time with the -x flag to extract files, and the -f flag to specify the name of the archive file.

For example, to extract the files from the “myfiles.tar” archive we created earlier, you would use the following command:

tar -xf myfiles.tar

This will extract all the files and directories from the archive file into the current directory.

Extracting with gzip

If your archive file was compressed using the gzip algorithm, you can extract it using the -z flag:

tar -xzf myfiles.tar.gz

This will decompress the archive file and extract its contents into the current directory.

Extracting with bzip2

Similarly, if your archive file was compressed using the bzip2 algorithm, you can extract it using the -j flag:

tar -xjf myfiles.tar.bz2

This will decompress the archive file and extract its contents into the current directory.

Other Useful Tar Commands

In addition to creating and extracting archive files, tar also has a few other useful commands that can come in handy when working with files on Linux.

Listing Contents of an Archive

To view the contents of an archive file without extracting it, you can use the -t flag:

tar -tf myfiles.tar

This will list all the files and directories contained in the archive file.

Appending Files to an Archive

If you want to add more files to an existing archive, you can use the -r flag:

tar -rf myfiles.tar file3.txt

This will add the file “file3.txt” to the “myfiles.tar” archive.

Updating an Archive

If you want to update an existing archive with newer versions of files, you can use the -u flag:

tar -uf myfiles.tar file1.txt

This will update the “file1.txt” file in the “myfiles.tar” archive with the newer version.

Conclusion

In this article, we’ve covered the basics of using the tar command on Linux to compress and extract files. We’ve explored what tar is, how it works, and how you can use it to compress and extract files on your Linux system.

By using the tar command, you can easily create archive files and compress them using various algorithms to reduce their size. This makes it easier to store and transfer large amounts of data, making it a useful tool for any Linux user.

Gulrukh Ch

About the Author: Gulrukh Ch

Gulrukh Chaudhary, an accomplished digital marketer and technology writer with a passion for exploring the frontiers of innovation. Armed with a Master's degree in Information Technology, Gulrukh seamlessly blends her technical prowess with her creative flair, resulting in captivating insights into the world of emerging technologies. Discover more about her on her LinkedIn profile.