Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#zip-tar-cheatsheet

Zip & Tar Cheatsheet

Archiving and compressing files is a daily task in Linux. tar is the standard tool for creating archives, while gzip or xz are used for compression. zip is commonly used for cross-platform compatibility with Windows.

Tar (Tape Archive)

tar creates an archive file, optionally compressing it.

Common Flags:

  • -c : Create
  • -x : Extract
  • -v : Verbose (show progress)
  • -f : File name (must be the last flag)
  • -z : Compress with gzip (.tar.gz)
  • -j : Compress with bzip2 (.tar.bz2)
  • -J : Compress with xz (.tar.xz)

Creating Archives

# Create an uncompressed archive
tar -cvf archive.tar folder/
 
# Create a gzip compressed archive (Most Common)
tar -czvf archive.tar.gz folder/
 
# Create an xz compressed archive (Smaller file size, slower to compress)
tar -cJvf archive.tar.xz folder/

Extracting Archives

# Extract an uncompressed archive
tar -xvf archive.tar
 
# Extract a gzip archive (.tar.gz or .tgz)
tar -xzvf archive.tar.gz
 
# Extract an xz archive (.tar.xz)
tar -xJvf archive.tar.xz
 
# Extract to a specific directory
tar -xzvf archive.tar.gz -C /path/to/destination/

Listing Contents

# List contents without extracting
tar -tvf archive.tar.gz

Zip and Unzip

zip creates compressed zip archives, widely used in Windows environments. You may need to install the zip and unzip packages first (sudo apt install zip unzip).

Creating Zip Archives

# Compress a single file
zip archive.zip file.txt
 
# Compress a directory recursively (-r)
zip -r archive.zip folder/
 
# Zip multiple files and directories
zip -r archive.zip file1.txt file2.txt folder/

Extracting Zip Archives

# Extract into current directory
unzip archive.zip
 
# Extract to a specific directory (-d)
unzip archive.zip -d /path/to/destination/

Listing Contents

# List contents without extracting
unzip -l archive.zip