Progress: 0/62 (0%)

📦 Creating Archives (tar)

Creating Archives

Creating archives is like packing multiple items into one suitcase so you can move or share them easily. In Linux, the most common tool for this is tar. It can combine multiple files/folders into a single archive, and it can also compress them at the same time.

Using tar

tar stands for tape archive (originated from old tape backups). It can create a single file containing many files and directories, without necessarily compressing them.

tar -cvf archive.tar file1.txt file2.txt

This command uses flags:

  • c → create a new archive
  • v → verbose (shows what's happening)
  • f → filename of the archive

This creates archive.tar containing file1.txt and file2.txt.

Creating Compressed Archives (tar -czvf)

You can compress the archive at the same time using gzip:

tar -czvf archive.tar.gz file1.txt file2.txt

This command adds the z flag:

  • z → compress using gzip

This produces a smaller file archive.tar.gz.

Extracting Compressed Archives (tar -xzvf)

To get files back from a compressed archive:

tar -xzvf archive.tar.gz

This command uses flags:

  • x → extract files
  • z → decompress gzip
  • v → verbose
  • f → filename

Quick Tips

If your archive is just .tar (not compressed), use tar -xvf archive.tar to extract.

If compressed (.tar.gz), use tar -xzvf archive.tar.gz.

Practical Example: Backing up a folder called project:

tar -czvf project_backup.tar.gz project/

This creates a single compressed archive of your whole project folder.

To restore it:

tar -xzvf project_backup.tar.gz

Real-life analogy

For tar: Like putting a bunch of books into a cardboard box - it's easier to carry, but the box isn't "smaller" yet. For compressed archives (tar -czvf): It's like putting your books in the cardboard box and then vacuum-packing the box to save space. For extracting (tar -xzvf): Unpacking your vacuum-packed suitcase and taking out the books. This wraps up File Compression and Archiving!