DEV Community

Kervie Sazon
Kervie Sazon

Posted on

Linux Fundamentals - Part 14: Archiving & Compressing Files

Archiving

Archiving means combining multiple files into one single file. It does not reduce file size, it simply packages everything together.

tar - command use to create the archive.

I practiced creating an archive using:

tar -cvf archive.tar proj_dir
Enter fullscreen mode Exit fullscreen mode

What -cvf means:

  • c - tells tar to create a new archive.
  • v - verbose (show progress).
  • f - file name. This creates one file called archive.tar that contains everything inside proj_dir.

Compression

Compression reduces files size to save storage space and make file transfers faster.

gzip - command use to compress file size.

Example:

gzip archive.tar
Enter fullscreen mode Exit fullscreen mode

This command will compress archive.tar and rename it to archive.tar.gz. The original archive.tar file will be replaced by the compressed version.

This created:

archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

Archiving + Compressing Together

What I found really useful is that we can archive and compress at the same time:

tar -czvf archive.tar.gz sample_dir
Enter fullscreen mode Exit fullscreen mode

The new option here is:

z - use gzip compression

This command:

  1. Combines the folder
  2. Compresses it
  3. Creates a .tar.gz file

Now I understand why .tar.gz files are so common in Linux systems.

Extracting Files

To extract a compressed archive:

tar -xzvf archive.tar.gz
Enter fullscreen mode Exit fullscreen mode
  • x = extract
  • z = gzip
  • v = verbose
  • f = file This restores everything from the archive.

Today, I learned that archiving combines multiple files into one using tar without reducing file size, while compression reduces the file size with tools like gzip. I practiced creating an archive with tar -cvf archive.tar proj_dir and compressing it using gzip archive.tar to make archive.tar.gz. I also learned that I can archive and compress at the same time with tar -czvf archive.tar.gz sample_dir. Finally, I practiced extracting files using tar -xzvf archive.tar.gz.

Top comments (0)