๐ฆ Tar Command¶
What is tar?¶
tar (short for tape archive) is used to bundle multiple files into one archive file. Itโs often combined with compression (like gzip) to reduce size.
Common Flags¶
-xโ extract files-cโ create an archive-vโ verbose, show progress-fโ file name, tells tar which archive file to work on-zโ gzip, used if the archive ends with.gz-Cโ change directory before extracting/creating
Understanding -f¶
-fmeans:
โThe next argument is the filename of the archive.โ
โ Example:
tar -xzvf archive.tar.gz
โ Wrong (no -f):
tar -xzv archive.tar.gz
Tar will get confused because it doesnโt know archive.tar.gz is the file to work on.
When is -f not needed?¶
- Only when reading/writing via stdin/stdout (e.g., using pipes).
Example:
curl -L https://example.com/archive.tar.gz | tar -xzv
Here tar doesnโt need -f because itโs reading directly from the pipe instead of a file.
๐ Rule of Thumb: Always use -f when working with a local archive file.
File Types¶
.tarโ archive only (no compression).tar.gzor.tgzโ archive + gzip compression
Extracting¶
- From
.tar(no compression):
tar -xvf archive.tar
- From
.tar.gz:
tar -xzvf archive.tar.gz
- Extract into a specific directory:
tar -xzvf archive.tar.gz -C /target/directory
Creating¶
- Create
.tar:
tar -cvf archive.tar file1 file2 dir1/
- Create
.tar.gz:
tar -czvf archive.tar.gz file1 file2 dir1/
โ Common Mistakes¶
- Wrong:
tar Cxzvf /usr/local archive.tar.gz
๐ C is misplaced; tar thinks itโs a file.
- Correct:
tar -xzvf archive.tar.gz -C /usr/local
๐ง Quick Memory Trick¶
Think of tar as โaction โ details โ file โ whereโ:
-xzvf [WHAT to extract] -C [WHERE to put it]
๐ Real-World Analogy¶
Think of tar like packing and opening gifts:
.tar= ๐ A box containing multiple items (but the box is full size)..tar.gz= ๐๐ฆ A box that has been vacuum-sealed (smaller size).-x= You open the box.-c= You pack items into the box.-z= You shrink/expand the vacuum seal.-f= You point to which box youโre opening.-C= You decide where in the house you want to place the opened items.