Compress Directory with Tar

February 4, 2024

Compression

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Options Explained

Option Description
-c Create an archive
-z Compress the archive with gzip
-v Display progress in terminal (verbose mode)
-f Specify the filename of the archive

Extraction

tar -xzvf archive.tar.gz

Common Use Cases

# Compress multiple directories
tar -czvf backup.tar.gz dir1 dir2 dir3

# Compress excluding certain files
tar -czvf archive.tar.gz --exclude='*.log' directory/

# Extract to specific directory
tar -xzvf archive.tar.gz -C /target/directory/

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

Best Practices

  • Use meaningful archive names
  • Include date in backup archives
  • Verify archive contents after creation
  • Check available disk space before compression
  • Use verbose mode for better tracking
#Linux #CLI #Compression #Tar