[Linux] How to Compress Folder and Decompress File

1. Compress a folder

To compress a folder in Linux, you can use the tar command. The tar command is used to create, manage, and extract files that are archived in the tar format. To compress a folder, you can use the -czvf flags, which tell tar to create a compressed archive, use gzip compression, be verbose, and use the file name that follows.

First, navigate to the directory containing the folder you want to compress. Then, run the following command:

tar -czvf foldername.tar.gz foldername

This will create a compressed tar archive of the folder with the name foldername.tar.gz. The .tar.gz extension indicates that the archive is a tar file that has been compressed using gzip.

If you want to compress the folder using a different compression algorithm, such as bzip2 or xz, you can use the -j or -J flags instead of the -z flag:

tar -cjvf foldername.tar.bz2 foldername

This will create a compressed tar archive using bzip2 compression, with the name foldername.tar.bz2.

tar -cJvf foldername.tar.xz foldername

This will create a compressed tar archive using xz compression, with the name foldername.tar.xz.

Note: Be sure to replace “foldername” with the actual name of the folder you want to compress.

2. Decompress a file

To use tar to decompress a file, you can use the -x flag, which tells tar to extract the contents of an archive. The -v flag enables verbose output, which will show the names of the files as they are extracted. The -f flag specifies the file name of the archive that you want to extract.

For example, to extract a tar archive named archive.tar, you can use the following command:

tar -xvf archive.tar

This will extract the contents of the archive.tar file into the current directory.

If the tar archive is compressed using gzip, you will need to use the -z flag to tell tar to decompress the file using gzip. For example, to extract a gzip-compressed tar archive named archive.tar.gz, you can use the following command:

tar -xzvf archive.tar.gz

This will extract the contents of the archive.tar.gz file and save them to the current directory.

If the tar archive is compressed using bzip2, you will need to use the -j flag to tell tar to decompress the file using bzip2. For example, to extract a bzip2-compressed tar archive named archive.tar.bz2, you can use the following command:

tar -xjvf archive.tar.bz2

This will extract the contents of the archive.tar.bz2 file and save them to the current directory.

If the tar archive is compressed using xz, you will need to use the -J flag to tell tar to decompress the file using xz. For example, to extract a xz-compressed tar archive named archive.tar.xz, you can use the following command:

tar -xJvf archive.tar.xz

This will extract the contents of the archive.tar.xz file and save them to the current directory.

Note: Be sure to replace “archive” with the actual name of the tar archive that you want to extract.


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
Product Image

Learn More