Linux Tar (Tape Archive) Usage Examples
Last updated:Table of Contents
- Compress directory with gzip, keep name
- Compress file with gzip
- Extract gz file
- Extract tar.gz file
- Extract bzip file
- Extract tar.bz2 file
- Extract gzip file into directory with same name
These are some example involving the usage of the tar command.
Compress directory with gzip, keep name
After compressing, the original directory is not deleted
Usage: tar -czf <output-file-name> <directory-name>
Example: compress a directory called my-dir into my-dir.tar.gz
$ tar -czf my-dir.tar.gz my-dir
Compress file with gzip
You can use either tar -czf or gzip commands:
With
tar$ tar -czf archive.tar.gz my-file.txtWith
gzip(create a file called my-file.txt.gz and remove original file)$ gzip my-file.txt
Extract gz file
(for example, the one created in the previous example)
$ tar -xzf file.tar.gz
Extract tar.gz file
$ tar -xzf file.tar.gz
Extract bzip file
Bzip files usually have a bz2 extension
$ tar -jxf your-archive-file.tar.bz2
Extract tar.bz2 file
Same as above:
$ tar -jxf your-archive-file.tar.bz2
Extract gzip file into directory with same name
Example: extract some-archive.tar.gz into some-archive/ directory
$ tar -xzf some-archive.tar.gz -C .
$ ls
some-archive.tar.gz some-archive/