We can decompress and unpack the archive using tar -zxvf archivename.tar.gz
command to decompress and unpack a .tar.gz file. This command extracts the files into the current directory.
Key takeaways
gzip is a tool for compressing individual files using the DEFLATE algorithm.
gzip
alone cannot compress directories. Instead, usetar
to create an archive of the directory, and then applygzip
to compress the archive. This can be done in one step usingtar -z
:
Use
tar -zcvf filename.tar.gz directory_name
command
tar -z
efficiently reduces file size and transfer times while preserving the directory structure.
gzip is a file compression tool by Jean-Loup Gailly and Mark Adler that uses the DEFLATE algorithm (a combination of LZ77 and Huffman coding). It is effective for compressing individual files but does not support directory structures by itself. On the other hand, the tar
command is used to generate archive files. It can combine several files and directories into a single file. When combined with the gzip
command, it enables both archiving and compression.
Compression reduces file size, whereas archiving combines multiple files into a single one. The tar
command is used for archiving files, while gzip
command is used for compressing them.
The gzip
command in Linux can only be used to compress a single file. In order to compress a directory, the combination of the tar + gzip
command (which is basically tar -z
) is used.
Let’s have a look at how to use tar -z
command to compress an entire directory in Linux.
Compressing the directories simplifies file management, reduces storage requirements, and makes directory transfers quick and easy. Lossless data compression ensures that the original data can be extracted from the compressed copy.
Suppose we have a myfolder
directory whose contents are as follows:
myfolder/.DS_Storemyfolder/LOGmyfolder/extension.xmlmyfolder/LICENSEmyfolder/myfolder.ymlmyfolder/README.txt
To compress the myfolder
directory, we run the following tar -z
command:
tar -zcvf myfolder.tar.gz myfolder
tar
: The command for archiving.
-z
: Compress the archive using gzip.
-c
: Create a new archive.
-v
: Verbose mode; lists files being archived.
-f
: Specifies the filename for the archive.
myfolder.tar.gz
: The name of the resulting compressed file.
myfolder
: The directory we want to compress.
Note that the order of the option flags matters. For example, -f
expects the name of the file immediately after it. So adding another flag after it instead of the file name would not give the expected results.
To view the contents of the myfolder.tar.gz
directory, we run the following command:
tar -tf myfolder.tar.gz
-t
: List the contents of the archive.
-f
: Specifies the filename for the archive.
The output of the above command would be:
my_folder/myfolder/._.DS_Storemyfolder/.DS_Storemyfolder/._LOGmyfolder/LOGmyfolder/._extension.xmlmyfolder/extension.xmlmyfolder/._LICENSEmyfolder/LICENSEmyfolder/._myfolder.ymlmyfolder/myfolder.ymlmyfolder/._README.txtmyfolder/README.txt
We can gzip the multiple directories at once by listing all directories in the following way:
tar -zcvf multiplefolders.tar.gz dir1/ dir2/ dir3/
We can specify the compression level with tar -z
as follows:
tar -cvf - myfolder | gzip -9 > myfolder.tar.gz
-
operator: Sends the archive output to standard output, so it can be used by another command right after.
|
pipe operator: Send the output of left-hand side command as the input to the right-hand side command.
gzip -9
: Compresses data with the highest compression level (9) using gzip.
We've provided a directory named gzip
in the following terminal. Please execute the tar -zcvf
command to compress Linux directory named gzip
directory and display its contents.
Let's quickly test your understanding of gzipping a directory in Linux in the following quiz:
Quiz!
What does the gzip
command do in Linux?
Archives files
Manages directories
Compresses individual files
Extracts .tar
files
Haven’t found what you were looking for? Contact Us
How do we decompress and unpack the archive?
What is tar and gz?
Does gzip work on directories?
How do I create a .gz folder in Linux?
What is the gzip command?
How do we zip a whole directory in Linux?