These notes were taken when making some changes for cncf/cnf-testsuite. I had to understand the options being used with the tar command before making the changes.

These commands work fine on linux. But if on Mac, install gnu-tar with brew to get these commands working as expected.

Extract and compress

# hello is a directory
$ ls .
hello tryouts somedir

# Compress
$ tar czvf hello.tar.gz hello

# Change to a directory to experiment
$ cd tryouts && mv ../hello.tar.gz .

# Extract
$ tar zxvf hello.tar.gz
$ ls
hello

# Cleanup
$ cd .. && rm -rf tryouts

Change directory when compressing

# hello has two directories
$ ls hello
world greeting

# Compress, with change directory option
$ tar czvf hello.tar.gz -C hello .

# Change to a directory to experiment
$ cd tryouts && mv ../hello.tar.gz .

# Extract
$ tar zxvf hello.tar.gz
$ ls
hello.tar.gz world greeting

# Notice magic? No parent directory when extracting

Compress only select directories/files

# hello has two directories
$ ls hello
world greeting

# Compress, with change directory option and select world dir only
$ tar czvf hello.tar.gz -C hello world

# Change to a directory to experiment
$ cd tryouts && mv ../hello.tar.gz .

# Extract
$ tar zxvf hello.tar.gz
$ ls
hello.tar.gz world

# See? No greeting dir because we did not compress it