linux zip files

Linux Basics: Working with tar, zip, and Other Compression and Archive Tools


Managing files and directories in Linux often involves compressing and decompressing archives. It’s an important skill, as it can save valuable disk space, make files easier to send, and can often be a necessity when installing software. This article explores how to work with archives and compression in Linux, using commands such as tar, gzip, gunzip, zip, and unzip.

TL;DR

Linux provides a wide variety of tools for creating, managing, compressing, and decompressing archives. The most commonly used commands include tar, gzip, gunzip, zip, and unzip, which are fundamental for efficient disk space usage and data transfer.

Creating and Extracting Archives with Tar

tar, which stands for “tape archive,” is a powerful utility in Linux used to create and extract archives. Here are a few examples:

  • To create a tar archive of a directory, use:

tar cvf archive_name.tar directory_name/

  • To extract a tar archive, use:

tar xvf archive_name.tar

Compression and Decompression with gzip and gunzip

gzip (GNU zip) and gunzip (GNU unzip) are used to compress and decompress files in Linux.

  • To compress a file using gzip, run:

gzip filename

  • To decompress a .gz file using gunzip, run:

gunzip filename.gz

Creating and Extracting Archives with Zip and Unzip

zip and unzip are straightforward commands used to create and extract .zip archives.

  • To create a zip archive, use:

zip archive_name.zip file1 file2

  • To extract a zip archive, use:

unzip archive_name.zip

Additional Commands to Help

  • To create a tar archive of multiple directories, you can list them one after the other:

tar cvf archive_name.tar directory1/ directory2/

  • If you want to compress a tar archive using gzip right after its creation, you can use the ‘z’ option:

tar cvzf archive_name.tar.gz directory_name/

  • Similarly, to extract a gzipped tar archive, use:

tar xvzf archive_name.tar.gz

  • gzip can also be used to compress a tar archive after its creation:

gzip archive_name.tar

This will create a file named archive_name.tar.gz.

  • To decompress a file with gunzip and keep the original file, use the ‘-k’ option:

gunzip -k filename.gz

  • With zip, you can also archive and compress entire directories. Here’s how:

zip -r archive_name.zip directory_name/

The ‘-r’ option is for recursion, which means that zip will include all subdirectories of the specified directory.

  • If you want to extract only certain files from a zip archive, you can list them after the archive name:

unzip archive_name.zip file1 file2

These additional examples should provide more context and usage scenarios for the commands covered in the article.

Conclusion

Understanding how to work with compression and archives in Linux is essential for any system administrator. The tar, gzip, gunzip, zip, and unzip commands are among the most commonly used tools for this purpose. Be sure to refer back to this article or use the man command in Linux to get more information on these tools.

To learn more about other Linux basics, check out our full Linux Basics Series on PureVoltage.


Posted

in

,

by

Tags: