Linux Tar (Tape Archive) Usage Examples

Last updated:

These are some example involving the usage of the tar command.

Archive and compress a directory using gzip

Say you want to compress a directory called my-dir

  • create a file called my-dir.tar.gz
   $ touch my-dir.tar.gz
  • use the file you've created to create the actual archive (the original directory is not changed)
   $ tar -czf my-dir.tar.gz my-dir

Extract gzip file

(for example, the one created in the previous example)

$ tar -xzf my-dir.tar.gz

Extract bzip file

$ tar -jxf your-archive-file.tar.bz2

Extract a gzip file into a new directory with the same name

$ ls 
$ tar -xzf some-archive.tar.gz -C .
$ ls
some-archive.tar.gz some-archive/

Dialogue & Discussion