How to tar untar and zip files
Hello and welcome back to my blog! Today we're going to talk about a very simple utility that allows you to compress and zip files and/or directories and unzip them when needed.
Installation of the required tools
CentOS or RHEL machines
If you are running CentOS or RHEL, then you will need to install the following tools using the yum
package manager. Use the following commands:
yum install tar gzip zip unzip bzip2
Ubuntu or debian machines
If you are running an ubuntu or a debian machine, use the apt-get
package installer to install the following tools.
apt-get install tar gzip zip unzip bzip2
Working with tar files
The TAR file format was used very early on for archiving files and it does not include any active compression by default. Folks usually tar files and then compress it using gzip. The extension for TAR file are .tar
.
Put a directory into a tar file
Use the following command in order to tar an entire directory.
tar cvfz FILENAME.tar.gz DIRECTORY/
Command Flags Explanation
c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
Put a Directory into a TAR file and Compress it with GZIP
Execute the following to create a single .tar.gz file containing all of the contents of the specified directory.
tar cvfz FILENAME.tar.gz DIRECTORY/
Command Flags Explanation
c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
z: Compress the TAR file with GZIP
Put a Directory into a TAR file and Compress it with BZIP2
BZIP2 compression generates smaller files than those with GZIP, but you tradeoff more processing power to achieve this. The following command gives you an example.
tar cvfj FILENAME.tar.bz2 DIRECTORY/
Extract items from tar files
Execute the following command to extract files and directories from an uncompressed .tar file.
tar xvf FILE.tar
Command Flags Explanation
x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
Extract items from gzipped tarball files
Use the following commands in order to extract from tar files that is compressed using gzip.
tar xvfz FILE.tar.gz
Command Flags Explanation
x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
z: Uncompress the tarball via GZIP.
Extract items from BZIPPED tarball files
Use the following commands in order to extract from tar files that is compressed using bzip2.
tar xvfj FILE.tar.bz2
Command Flags Explanation
x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
j: Uncompress the tarball via BZIP2.
Reference
All the credit for the knowledge shared here goes to the writer of the blog linked here: how to tar and untar zip files