What command do I need to unzip/extract a .tar.gz file?
I received a huge .tar.gz file from a client that contains about 800 mb of image files (when uncompressed.) Our hosting company’s ftp is seriously slow, so extracting all the files locally and sending them up via ftp isn’t practical. I was able to ftp the .tar.gz file to our hosting site, but when I ssh into my directory and try using unzip, it gives me this error:
What command do I need to use to extract all the files in a .tar.gz file?
8 Answers 8
Type man tar for more information, but this command should do the trick:
Also, to extract in a specific directory
for eg. to extract the archive into a custom my_images directory .
To explain a little further, tar collected all the files into one package, community_images.tar . The gzip program applied compression, hence the gz extension. So the command does a couple things:
- f : this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.
- z : tells tar to decompress the archive using gzip
- x : tar can collect files or extract them. x does the latter.
- v : makes tar talk a lot. Verbose output shows you all the files being extracted.
- C : means change to directory DIR . In our example, DIR is my_images .
How To Extract .tar.gz Files using Linux Command Line
By Jithin on January 5th, 2017
In this tutorial we can learn how to extract tar.gz files using Linux Command line tools.
A .tar.gz file is nothing, but an archive. It is a file that acts as a container for other files. The tar program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update, or list files which were already stored. An archive can contain many files, folders, and subfolders, usually in compressed form using gzip or bzip2 program on Unix operating systems. Initially, tar archives were used to store files conveniently on magnetic tape. The name “Tar” comes from this use; it stands for tape archiver. You need to use the tar command which can create and manipulate archive files in .tar.gz under Unix like operating systems. It is very useful for archiving multiple files together into a single archive file. It allows us to restore files individually. Tar can direct its output to available devices, files, or other programs (using pipes), it can even access remote devices or files (as archives). tar.gz file is actually the product of two different things. Tar basically just packages a group of files into a single file bundle, but doesn’t offer compression on it’s own. To compress tar you’ll want to add the highly effective gzip compression. In this documentation, we can discuss about how to extract the tar.gz files from the command line. For this, open a command-line terminal and then type the following commands to open and extract a .tar.gz file.
Extracting .tar.gz files
1) If your tar file is compressed using a gzip compressor, use this command to uncompress it.
x: This option tells tar to extract the files.
v: The “v” stands for “verbose.” This option will list all of the files one by one in the archive.
z: The z option is very important and tells the tar command to uncompress the file (gzip).
f: This options tells tar that you are going to give it a file name to work with.
A tarball is a group or archive of files that are bundled together using the tar command and have the .tar file extension.
2) To uncompress tar.gz file into a different directory, use the command below:
$ tar xvzf file.tar.gz -C /path/to/somedirectory
Where the -C argument is used to specify the path to place the file. By defaults files will be extracted into the current directory. To change the directory, we use -C option.
3) To extract test.doc file from the file.tar.gz tarball, use the following command:
4) To view a detailed table of content by listing all files in archive, you can use the following command:
5) To extract the .gz file without tar, you can use the following command:
Extracting .tar.bz2 files
This is just about the same as the gzip decompression. The major difference is that the z option has been replaced by the j option.
If your tar file is compressed using a bZip2 compressor, use the following command to extract it.
Where ‘j’ will decompress a bzip2 file.
If you need any further assistance please contact our support department.
12 Responses to “How To Extract .tar.gz Files using Linux Command Line”
Nice explanation. Very helpful. Thanks a lot.
How to Extract (Unzip) Tar Gz File
Updated Sep 24, 2019
If you are roaming the open-source world, chances are you encounter .tar.gz files on a regular basis. Open-source packages are generally available to download in .tar.gz and .zip formats.
The tar command is used to create tar archives by converting a group of files into an archive. It supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. Tar was originally designed for creating archives to store files on magnetic tape which is why it has its name “Tape ARchive”.
Gzip is the most popular algorithm for compressing tar files. By convention, the name of a tar archive compressed with gzip should end with either .tar.gz or .tgz.
In short, a file that ends in .tar.gz is a .tar archive compressed with gzip.
The tar command can also be used to extract tar archives, display a list of the files included in the archive, add additional files to an existing archive, as well as various other kinds of operations.
In this tutorial, we will show you how to extract (or unzip) tar.gz and tgz archives.
Extracting tar.gz File #
Most Linux distributions and macOS comes with tar command pre-installed by default.
To extract a tar.gz file, use the —extract ( -x ) option and specify the archive file name after the f option:
The tar command will auto-detect compression type and will extract the archive. The same command can be used to extract tar archives compressed with other algorithms such as .tar.bz2 .
If you are a Desktop user and the command-line is not your thing you can use your File manager. To extract (unzip) a tar.gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files.
The -v option will make the tar command more visible and print the names of the files being extracted on the terminal.
By default, tar will extract the archive contents in the current working directory . Use the —directory ( -C ) to extract archive files in a specific directory:
For example, to extract the archive contents to the /home/linuxize/files directory, you can use:
Extracting Specific Files from a tar.gz File #
To extract a specific file(s) from a tar.gz file, append a space-separated list of file names to be extracted after the archive name:
When extracting files, you must provide their exact names including the path, as printed by —list ( -t ).
Extracting one or more directories from an archive is the same as extracting files:
If you try to extract a file that doesn’t exist, an error message similar to the following will be displayed:
You can also extract files from a tar.gz file based on a wildcard pattern, by using the —wildcards option and quoting the pattern to prevent the shell from interpreting it.
For example, to extract files whose names end in .js (Javascript files), you would use:
Extracting tar.gz File from stdin #
If you are extracting a compressed tar.gz file by reading the archive from stdin (usually through a pipe), you need to specify the decompression option. The option that tells tar to read the archives through gzip is -z .
In the following example we are downloading the Blender sources using the wget command and pipe its output to the tar command:
If you don’t specify a decompression option, tar will indicate which option you should use:
Listing tar.gz file #
To list the content of a tar.gz file, use the —list ( -t ) option:
The output will look something like this:
If you add the —verbose ( -v ) option, tar will print more information, such as owner, file size, timestamp ..etc:
Conclusion #
tar.gz file is a Tar archive compressed with Gzip. To extract a tar.gz file, use the tar -xf command followed by the archive name.
If you have any questions, please leave a comment below.