Lesson 17: tar — archiving & compression
Your production server is accumulating logs. /var/log/app/ has grown to 50GB and you need to free space. tar czf logs.tar.gz /var/log/app/ creates a gzip-compressed archive — one file instead of thousands of small ones. tar tzf checks what's inside without extracting — verify before you delete. tar
tar is like packing items into a box (c), peeking inside without opening it (t), and opening it in another room (x). z = squeezes out air (gzip). f = the box's name.
- tar czf
- Creates a gzip-compressed archive. c = create, z = gzip, f = file name. tar czf archive.tar.gz dir/ creates archive.tar.gz containing dir/ without deleting it.
- tar xzf
- Extracts a gzip archive. x = extract, z = gzip, f = file name. Default: extracts to the current directory. With -C /path/ — extracts to the specified path. The path must already exist.
- tar tzf
- Lists the contents of a gzip archive without extracting. t = table of contents, z = gzip, f = file name. Useful to verify before extracting or deleting the source.
- gzip compression (-z)
- The -z flag adds gzip compression to a tar archive. .tar without -z is not compressed — just packaged. .tar.gz with -z is compressed — significantly smaller. Log files typically compress 70-90%.