Compress and Decompress tar Archives
In this step, we will learn how to compress and decompress tar archives using different compression methods.
First, let's create a tar archive and compress it using the bzip2
utility:
cd ~/project
mkdir uncompressed
touch uncompressed/file1.txt uncompressed/file2.txt uncompressed/file3.txt
tar -cjf archive.tar.bz2 uncompressed/
Example output:
uncompressed/file1.txt
uncompressed/file2.txt
uncompressed/file3.txt
In this example, we used the -j
option to compress the archive using bzip2. The resulting file is archive.tar.bz2
.
Now, let's extract the contents of the bzip2 compressed archive:
tar -xjf archive.tar.bz2
Example output:
uncompressed/file1.txt
uncompressed/file2.txt
uncompressed/file3.txt
The -x
option extracts the files from the archive, and the -j
option decompresses the archive using bzip2.
Next, let's create a tar archive and compress it using the xz
utility:
cd ~/project
mkdir compressed_xz
touch compressed_xz/file1.txt compressed_xz/file2.txt compressed_xz/file3.txt
tar -cJf archive.tar.xz compressed_xz/
Example output:
compressed_xz/file1.txt
compressed_xz/file2.txt
compressed_xz/file3.txt
In this example, we used the -J
option to compress the archive using xz. The resulting file is archive.tar.xz
.
Finally, let's extract the contents of the xz compressed archive:
tar -xJf archive.tar.xz
Example output:
compressed_xz/file1.txt
compressed_xz/file2.txt
compressed_xz/file3.txt
The -x
option extracts the files from the archive, and the -J
option decompresses the archive using xz.
The tar
command supports various compression methods, allowing you to choose the one that best suits your needs.