📦 Creating Archives (tar)
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.
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:
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:
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:
This creates a single compressed archive of your whole project folder.
To restore it:
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!