I have downloaded a file called foo.tar.gz from the Internets. How do I extract tar.gz file under Linux / UNIX like operating systems using command line options?
A tarball (tar.gz file) is nothing but compressed tar archive. 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. Initially, tar archives were used to store files conveniently on magnetic tape. The name “Tar” comes from this use; it stands for tape archiver. Despite the utility’s name, Tar can direct its output to available devices, files, or other programs (using pipes), it can even access remote devices or files as archives. The tar command is available on Linux (CentOS/RHEL/Fedora/Debian/Ubuntu and all other distros), BSD (OpenBSD/NetBSD/FreeBSD), Apple macOS, HP-UX, AIX, and other Unix-like operating systems. This page shows how to extract tar.gz file using command line.
Syntax to extract .tar.gz file
The syntax is as follows:
tar [options] file.tar.gz tar [options] file.tar.gz pattern tar -xf file.tar.gz tar -xvf file.tar.gz tar -zxvf file.tar.gz tar -zxvf file.tar.gz file1 file2 dir1 dir2
Extracting tr.gz. file
To extract one or more members from an archive, enter:$ tar -zxvf {file.tar.gz}
If your tarball name is backup.tar.gz, enter the following at a shell prompt to extract files:$ tar -zxvf backup.tar.gz
To extract resume.doc file from backup.tar.gz tarball, enter:$ tar -zxvf backup.tar.gz resume.doc
Where,
- -z : Work on gzip compression automatically when reading archives.
- -x : Extract tar.gz archive.
- -v : Produce verbose output i.e. display progress and extracted file list on screen.
- -f : Read the archive from the archive to the specified file. In this example, read backups.tar.gz archive.
- -t : List the files in the archive.
- -r : Append files to the end of the tarball.
- --delete (GNU/Linux tar only) : Delete files from the tarball.
In order to untar a tar file, the -x (for extract) and -f options are needed.
Extracting an entire archive
To extract an entire archive, specify the archive file name only, with no individual file names as arguments.tar -zxvf backup.tar.gz
List files in archive
To view a list of the files within a tarball, issue the following command, enter:$ tar -tvf backup.tar.gz
How to create a tarball
tar command used to create a Tape ARchive. The resulting file is known as a tarball in Unix world. Let us see how to create a tarball using tar command. The following would create an archive file called data.tar from the three files named file1.txt, file2.txt and file3.txt that are located in the current directory:tar -cvf data.tar file1.txt file2.txt file3.txt
One can use the tar command to make archives from the contents of one or more directories. For example, the contents of two directories named /etc/ and /home/vivek/Documents/ could be archived into a file named dump.tar with the following:tar -cvf dump.tar /etc/ /home/vivek/Documents
In this example, create an archive named data.tar.bz2 of the files /etc/ directory that is compressed using gzip:tar -cvzf files.tar.gz /etc/
How to add files to an existing tarball
Pass the -r option. For example, the following would append a file named resume.doc to file.tar:tar -rf file.tar resume.doc
How delete files from a tarball
Pass the --delete option to GNU/tar command which allows specified files to be completely removed from a tarball. Thus, for example, the files pic1.png and pic2.png can be removed from file.tar with the following:tar -f file.tar --delete pic1.png pic2.png
Conclusion
We have shown you how to extract tar.gz file from the command line. The commands should work on Linux and Unix-like systems. For more info see tar command help page here and here.