How to Extract a .tar.gz File Using Command Line in Ubuntu

Topic: Ubuntu / LinuxPrev|Next

Answer: Use the tar Command

You can use the tar command to unzip or extract a .tar.gz file using command line in Ubuntu.

Tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives. The .gz extension indicates the gzip program is used for compression.

tar -xvzf /path/to/filename.tar.gz

For example, if your current working directory is /var/www and you want to extract the file images.tar.gz inside of it, you can simply use the command:

tar -xvzf images.tar.gz

Alternatively, you can provide the absolute path for the file like this:

tar -xvzf /var/www/images.tar.gz

Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash / is an absolute path.

The options have the following meanings:

  • -x : Tells tar to extract files from an archive.
  • -v : Tells tar to verbosely list all the files being extracted.
  • -z : Tells tar to decompress the archive using gzip.
  • -f : Tells tar the name of the archive to operate upon. This must be specified as last option, and the tar file must be specified immediately after it.

Moreover, if you want to extract the tar file at different location you can use the option -C, which specifies change to directory before performing any operations.

tar -xvzf /path/to/filename.tar.gz -C /path/to/destination_folder

For example, if your current working directory is /var/www and you want to extract /var/www/images.tar.gz file inside /var/www/assets, you can run:

tar -xvzf images.tar.gz -C assets

Or, if you want to use absolute path you can run the following command.

tar -xvzf /var/www/images.tar.gz -C /var/www/assets

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates