Skip to content

Tar

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