Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Creating and Managing Archives in Linux

Learn how to create, manage, and compress Linux archives using key tools like tar, gzip, bzip2, and 7z, with step-by-step commands for effective data management.

Oct 8, 2024 • 5 Minute Read

Coding lines in Linux
  • Software Development
  • Guides

Welcome to the world of Linux archiving, where files become neatly packaged for storage, distribution, and preservation. Linux is a file-oriented OS, meaning many administrative tasks and hardware device interactions involve managing files. We'll explore the essential tools and techniques that empower Linux users to manage their data effectively.

From the classic tar archives to advanced compression algorithms like 7z, we'll navigate through the diverse array of options available. With this blog you'll gain the knowledge and skills needed to master archiving on Linux.

Using tar for Archiving

Tar, short for "tape archive," is a fundamental tool for archiving files on Linux systems. It allows you to bundle multiple files and directories into a single archive, which can then be compressed for efficient storage or transmission.

Create an Archive

To create an archive, use: tar -cf archive.tar /filestoarchive. Add the -v option to see the process. You need read permissions for the files and execute permissions for their directory.

In the example, file1.txt, file2.txt and file3.txt have been archived into the file archive.tar using the tar utility.

View an Archive Contents

To view the contents of a tar archive, use the command tar -tf archive.tar. This will list the files and directories within the archive without extracting them, as seen in the example below.

Extract an Archive

To extract an archive, use tar -xvf /archive.tar, which will extract the contents into the current directory.

Within the example we can see that the file*.txt files had been removed, which has been verified with the ls command. Using the extract command, then verifying again with ls we are now able to see the files back within the directory. 

Compressing Files with gzip and bzip2:

gzip and bzip2 are two popular compression utilities used to reduce the size of files and save disk space. These tools offer different compression ratios and speeds, providing flexibility to users based on their specific needs. In the examples above using tar not a single byte was compressed. In the past, after creating an archive, you had to compress it using a separate utility like gzip or bzip2

Nowadays both gzip and bzip2 offer almost the same level of compression. Historically bzip2 offered a better compression algorithm.

Within this screenshot you can see that the file archive.tar had an approximate size of 10,000 bytes (10k), which has been reduced to just 162 bytes after compression, a significant 98% decrease in size.

Decompressing Files with gunzip and bunzip2

gunzip and bunzip2 are command-line utilities used to decompress files that have been compressed using the gzip and bzip2 formats, respectively.

The gunzip utility is used to decompress files that have been compressed with gzip. It takes files with extensions like .gz and restores them to their original form.

Like gunzip, bunzip2 is a utility used to decompress files that have been compressed with bzip2. It handles files with extensions like .bz2.

You can see that the file archive.tar.gz has been unarchived using the gunzip utility, which has increased the file to its original size of approximately 10,000 bytes.

Archiving and Compressing Files with tar

Instead of using tar to archive a group of files into a single folder, and then using utilities such as gzip and bzip2 to compress it, you are able to do this within a single command.

Simply add the option -z to compress and archive with gzip or -j to compress a file with bzip2.

By using gzip within the tar command we are able to create a compressed archive file, with a size of just 150 bytes.

Using 7z for High Compression

7z (7-Zip) offers high compression ratios. It typically uses the .7z file extension. While its compression and decompression speeds are generally slower than those of gzip and bzip2, the compression ratio is superior. 7z also supports features like encryption, solid compression, and handling large files and directories. It's suitable for scenarios where disk space is a concern and the highest compression is needed, even if it takes more time.

Create an Archive

To create an archive using 7z, you use the a (add) command followed by the archive name and the files you want to include. For example, 7za a archive.7z file1.txt file2.txt file3.txt.

Within the example the 7z utility was used to create an archive of the 3 files. Using the ls command we are able to see that the file is just 143 bytes, slightly more compressed than previous examples in the blog using the bzip utility. For large files, this better compression utility will save significant space.

View Archive Contexts

To list the contents of an archive without extracting, you use the l (list) command followed by the archive name. This would be 7za l archive.7z.

Extract an Archive

To extract the contents of an archive, you use the e (extract) command followed by the archive name. The command is simply 7za e archive.7z.

Conclusion

This blog post serves as an introduction to archiving on Linux, exploring essential tools and techniques for effective data management. It covers classic methods like tar archives and advanced compression algorithms such as 7z

The post details how to create, view, and extract archives using tar, along with insights into compression utilities like gzip and bzip2, including their decompression counterparts, gunzip and bunzip2

Additionally, it highlights the benefits of 7z, known for its high compression ratios and advanced features like encryption and solid compression. 

I would encourage you to check out these courses on Pluralsight to learn more about working with archives on Linux, but also many other Linux topics that administrators should know:

 

Stephen Wenn

Stephen W.

Stephen Wenn is an accomplished IT consultant, who has amassed extensive expertise in designing, implementing, and overseeing network, security, virtual, cloud, and server infrastructure within dynamic and intricate technical environments for public and private sector organizations. Stephen holds many vendor certifications in Cisco, VMware, Microsoft, AWS, Juniper and many more, including CCIE #66053. When not involved with technology, Stephen enjoys fitness training, playing video games and spending time with his family.

More about this author