Mastering File and Directory Compression and Extraction in Linux

Mastering File and Directory Compression and Extraction in Linux

In this blog, we will be learning about how the files and directories are compressed and extracting those files in a Linux environment with some practical exposure.

Compression and extraction of files and directories are fundamental computing operations. They aid in disk space conservation, speed up file transfers, and effective data organization. We will examine the fundamentals of file and directory compression and extraction in Linux in this blog article, focusing on real-world applications using tools like tar, gzip, and zip.

File compression is the process of reducing the size of one or more files, making them take up less disk space.

1. Using gzip

Compression:

#compression:
gzip filename
#Decompression:
gunzip filename.gz

We can see here abc.txt file is compressed using the gzip command and we got abc.txt.gz file. Using gunzip we extracted the abc.txt.gz file to abc.txt.

2. Using tar and gzip

#Compression:
tar -czvf archive.tar.gz directory/
#Decompression:
tar -xzvf archive.tar.gz

options:

  • -c: Create a new archive.

  • -z: Compress the archive using gzip.

  • -v: Verbose mode (optional, provides detailed output).

  • -f: Specify the archive file name.

  • -x: Extract files from the archive.

Files are compressed into a file named archive.tar.gz. Let's decompress it.

The files have been successfully extracted from the archive.tar.gz file. The extraction process created a directory named home the current working directory, and within that directory, it reproduced the directory structure that was originally archived.

2. Using Zip

ZIP is another widely used compression format that can compress both individual files and directories. It offers a convenient way to create compressed archives with password protection.

#compression
zip compressed.zip filename
#decompression
unzip compressed.zip

Using the zip command we compressed all the files of the current directory(represented as *) into the zip file named my_archive.zip.

Here we unzip the contents of my_archive.zip into the folder named archive. We can see the contents of the archive directory same as that of the shell-scripts directory.

Compression and extraction of files and directories are essential abilities for effectively managing data in a Linux environment. The tools mentioned in this blog post, such as tar, gzip, and zip, provide versatile options for compressing and extracting files and directories. Learning how to use these tools will greatly improve your experience with Linux, whether you're archiving files for storage, transferring data, or simply organizing your system. Start mastering these skills by incorporating them into your regular Linux routine.

Happy Learning!!