Linux zip with tar

How to create and open zip, tar, tar.gz files in Linux

This guide will explain how to create and/or open and extract the contents of .ZIP, .TAR and .TAR.GZ files in the Linux operating system.

Background

Data compression has been extremely useful to us over the years. Whether it’s a zip file containing images to be sent in a mail or a compressed data backup stored on a server, we use data compression to save valuable hard drive space or to make the downloading of files easier. There are compression formats out there which allow us to sometimes compress our data by 60% or more. I’ll run you through using some of these formats to compress and decompress files and directories on a Linux machine. We’ll cover the basic usage of zip, tar, tar.gz and the tar.bz2 formats. These are some of the most popular formats for compression used on Linux machines.

Quick Note: If you’re looking for the Windows version of this tutorial, you can find it at How to Open tar.gz Files in Windows.

Before we delve into the usage of the formats I’d like to share some of my experience using the various formats of archiving. I’m talking about only a few data compression formats here, and there are many more out there. I’ve realized that I need two or three formats of compression that I’m comfortable using, and stick to them. The zip format is definitely one of them. This is because zip has become the de-facto standard choice for data compression, and it works on Windows as well. I use the zip format for files that I might need to share with Windows users. I like to use the tar.gz format for files that I would only use on my Mac and Linux machines.

ZIP Files in Linxu

Zip is probably the most commonly used archiving format out there today. Its biggest advantage is the fact that it is available on all operating system platforms such as Linux, Windows, and Mac OS, and generally supported out of the box. The downside of the zip format is that it does not offer the best level of compression. Tar.gz and tar.bz2 are far superior in that respect. Let’s move on to usage now.

Читайте также:  File system programming in linux

To compress a directory with zip do the following:

# zip -r archive_name.zip directory_to_compress

Here’s how you extract a zip archive:

# unzip archive_name.zip

TAR Files in Linux

Tar is a very commonly used archiving format on Linux systems. The advantage with tar is that it consumes very little time and CPU to compress files, but the compression isn’t very much either. Tar is probably the Linux/UNIX version of zip – quick and dirty. Here’s how you compress a directory:

# tar -cvf archive_name.tar directory_to_compress

And to extract the archive:

# tar -xvf archive_name.tar.gz

This will extract the files in the archive_name.tar archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:

# tar -xvf archive_name.tar -C /tmp/extract_here/

TAR.GZ Files in Linux

This format is my weapon of choice for most compression. It gives very good compression while not utilizing too much of the CPU while it is compressing the data. To compress a directory use the following syntax:

# tar -zcvf archive_name.tar.gz directory_to_compress

To decompress an archive use the following syntax:

# tar -zxvf archive_name.tar.gz

This will extract the files in the archive_name.tar.gz archive in the current directory. Like with the tar format you can optionally extract the files to a different directory:

# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

TAR.BZ2 Files in Linux

This format has the best level of compression among all of the formats I’ve mentioned here. But this comes at a cost – in time and in CPU. Here’s how you compress a directory using tar.bz2:

# tar -jcvf archive_name.tar.bz2 directory_to_compress

This will extract the files in the archive_name.tar.bz2 archive in the current directory. To extract the files to a different directory use:

# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

Data compression is very handy particularly for backups. So if you have a shell script that takes a backup of your files on a regular basis you should think about using one of the compression formats you learned about here to shrink your backup size.

Over time you will realize that there is a trade-off between the level of compression and the the time and CPU taken to compress. You will learn to judge where you need a quick but less effective compression, and when you need the compression to be of a high level and you can afford to wait a little while longer.

Источник

How to Tar Untar and Zip Files

This article will help you understand how you can handle the common file formats TAR, GZIP, BZIP and ZIP on Linux operating systems (including CentOS, Ubuntu) and even some Unix-based OSes like MacOS (OS X) via the command line either via SSH or a local terminal session.

How to Install the Needed Tools

On many Linux-like operating systems the command line tools for working with TAR, GZIP, BZIP and ZIP files are already installed, so you almost certainly don’t need to install anything, but if you are running a minimal installation of your OS or if you’ve removed the tools in the past, follow the directions below to install what you need. Select the tab for the OS you are running:

Читайте также:  Linux настройка принтера hp laserjet

The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.

  1. Pull up a terminal session or log into your server/computer via SSH.
  2. The base repositories for these OSes have the packages we need. Execute the following command:
yum install tar gzip zip unzip bzip2

Now you should be able to follow the rest of the directions in this article.

The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.

Example: sudo apt-get install .

  1. Pull up a terminal session or log into your server/computer via SSH.
  2. The base repositories for these operating systems have the packages we need. Execute the following command:
apt-get install tar gzip zip unzip bzip2

Now you should be able to follow the rest of the directions in this article.

The needed command line tools ship with every version of MacOS/OS X since at least 10.6 Snow Leopard (and may also be installed in earlier versions we were unable to test).

To work with files via the command line, open the Terminal application located in /Applications/Utilities/Terminal(.app).

Once the terminal is open, you will be able to follow the rest of the directions in this article.

Remember, you can drag and drop files or folders into the terminal application and the full path to those items will be pasted automatically into the command line.

Working with TAR Files

The TAR file format is a very early archiving format that doesn’t include any active compression by default. Often on Linux, items are tarred and then gzipped to compress them. TAR files typically end in .tar.

Put a Directory into a TAR File

Execute the following to create a single .tar file containing all of the contents of the specified directory:

tar cvf FILENAME.tar DIRECTORY/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a tarball.

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/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

Tarred files compressed with GZIP sometimes use the .tgz file extension.

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

Читайте также:  Включить графический интерфейс astra linux

Put a Directory into a TAR file and Compress it with BZIP2

Execute the following to create a single .tar.bz2 file containing all of the contents of the specified directory compressed with BZIP. (BZIP typically produces smaller files than GZIP, at the cost of more processing time.):

tar cvfj FILENAME.tar.bz2 DIRECTORY/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

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.
j: Compress the TAR file with BZIP2

Extract Items from TAR Files

Execute the following command to extract files and directories from an uncompressed .tar file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

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 to uncompress.

Extract Items from GZIPPED Tarball File

Execute the following command to extract files and directories from a GZIP compressed TAR file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

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 to uncompress.
z: Uncompress the tarball via GZIP.

Extract Items from BZIPPED Tarball File

Execute the following command to extract files and directories from a BZIP compressed TAR file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

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 to uncompress.
j: Uncompress the tarball via BZIP2.

If you’d rather specify a different directory to extract files to rather than just dumping everything in the current directory add -C /PATH/TO/DIRECTORY/ to the commands above. Replace /PATH/TO/DIRECTORY/ with the actual path to the directory where you want the files to be placed.

Working with Zip Files

Zip is probably the most common compressed archiving format in the world. Zip files usually end in .zip.

Compress a Directory Full of Files into a ZIP File

Execute the following command to place everything inside a specified directory into a compressed ZIP file.

Command Flags Explanation

-r: Recursively compress all files and directories contained within DIRECTORY/ in the zip file (otherwise you only get the top level files).

Uncompress a ZIP file Into the Current Directory

Execute the following command to uncompress the items in the ZIP file into the current directory.

Источник

Оцените статью
Adblock
detector