Linux Commands | Archive File Management | tar, tar.gz Compress and Extract

Compress with tar.gz

Command

tar -czvf {archive file name}.tar.gz {file to compress 1} {file to compress 2}...

Compress Files

The following command compresses a1.jpg, a2.jpg, and a3.jpg in the ./test directory into a file named images.tar.gz.

tar -czvf images.tar.gz ./test/a1.jpg ./test/a2.jpg ./test/a3.jpg

Compress a Directory

The following command compresses the ./test directory itself.

tar -czvf images.tar.gz ./test

Exclude a Specific File from Compression

If you want to compress a1.jpg and a2.jpg but exclude a3.jpg, use the command below.

tar -czvf --exclude=./test/a3.jpg images.tar.gz ./test

Extract tar.gz

Command

tar -xzvf {archive file name}.tar.gz

The following command extracts the images.tar.gz archive.

tar -xzvf images.tar.gz

Extract tar

To extract a tar archive, remove only the z option.

For example, the following command extracts an archive named images.tar.

tar -xvf images.tar