How to Compress and Extract Files With Tar Compress?

The tar command on the LINUX system is usually accustomed to produce .tar.gz or .tgz archive files, conjointly called “tarballs.” This command has an oversized variety of choices, however, you have simply got to bear in mind many letters to quickly produce archives with tar. The tar command can extract the ensuing archives, too.

The antelope tar command enclosed with LINUX system distributions has integrated compression. It will create a .tar archive so compress it with gzip or bzip2 compression during a single command. That is why the ensuing file could be a .tar.gz file or .tar.bz2 file.

Compress a complete directory or one file

Use the subsequent command to compress a complete directory or one file on the LINUX system. It will conjointly compress each different directory within a directory you specify–in different words, it works recursively.

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Here’s what those switches truly mean:

  • -c: Creates an archive.
  • -z: Compresses the archive with gzip.
  • -v: shows progress within the terminal whereas making the archive, conjointly referred to as “verbose” mode. The v is often non-obligatory in these commands, however, it’s useful.
  • -f: permits you to specify the filename of the archive.
compress files with tar compress

Let us say you have got a directory named “stuff” within the current directory and you wish to avoid wasting it on a file named archive.tar.gz. You would run the subsequent command:

tar -czvf archive.tar.gz stuff

Or, let us say there is a directory at /usr/local/something on this system and you wish to compress it to a file named archive.tar.gz. You will require to run the subsequent command:

tar -czvf archive.tar.gz /usr/local/something

Also read: Know all about .psd file

Compress multiple directories or files directly

While tar compress is usually accustomed to compressing a single directory, you may conjointly use it to compress multiple directories, multiple individual files, or both. Simply offer a listing of files or directories rather than one by one.

As an example, let us say you wish to tar compress the /home/ubuntu/Downloads directory, the /usr/local/stuff directory, and therefore the /home/ubuntu/Documents/notes.txt file. You should simply run the subsequent command:

tar -czvf archive.tar.gz /home/ubuntu/Downloads /usr/local/stuff /home/ubuntu/Documents/notes.txt

Just list as several directories or files as you wish to copy.

Exclude directories and files

In some cases, you would want to tar compress a complete directory, however not including certain files and directories. You can do that by appending an –exclude switch for every directory or file you wish to exclude.

For example, let us say you wish to compress /home/ubuntu, while you do not wish to tar compress the /home/ubuntu/Downloads and /home/ubuntu/.cache directories.

Also read: How to convert HTML to RSS feed?

Compress files with tar compress:

Here is how you can do it:

tar -czvf archive.tar.gz /home/ubuntu –exclude=/home/ubuntu/Downloads –exclude=/home/ubuntu/.cache

The –exclude switch is very powerful. It does not take the names of directories and files–it truly accepts patterns. There is a great deal additionally you would do with it. As an example, you may archive a complete directory and exclude all .mp4 files with the subsequent command:

tar -czvf archive.tar.gz /home/ubuntu –exclude=*.mp4

Use bzip2 compression instead

While gzip compression is most often accustomed to producing .tar.gz or .tgz files, tar conjointly supports bzip2 compression. This permits you to make bzip2-compressed files, typically named .tar.bz2, .tar.bz, or .tbz files. To do so, simply replace the -z for gzip within the commands here with a -j for bzip2.

Gzip is quicker, however, it typically compresses one bit less, thus you get a somewhat larger file. Bzip2 is slower, however, it compresses one bit additional, thus you get a somewhat smaller file. Gzip is additionally more common, with some minimal LINUX system/systems together with gzip supported by default, however not bzip2 support. In general, though, gzip and bzip2 are many equivalent factors and each can work equally.

For example, rather than the primary example, we tend to provide for clicking the ‘things’ directory, you would run the subsequent command:

tar -cjvf archive.tar.bz2 stuff

Extract an archive file

Once you have got an archive, you can extract it with the tar command. The subsequent command can extract the contents of archive.tar.gz to this directory.

tar -xzvf archive.tar.gz

It is an equivalent because the archive creation command is generally used on top of, except the -x switch replacing the -c switch. This specifies that you wish to extract an archive rather than producing one.

You may wish to extract the contents of the archive to a selected directory.

You will have to do so by appending the -C switch to the top of the command. As an example, the following command can extract the contents of the archive.tar.gz file to the /tmp directory.

tar -xzvf archive.tar.gz -C /tmp

If the file could be a bzip2-compressed file, replace the “z” within the on top of commands with a “j”.

This is the best potential usage of the tar compress command. The command includes an outsized range of extra choices, thus we tend to can’t presumably list all here. For a lot of data. run the info tar command at the shell to look at the tar command’s detailed information page. Press the q key to quit the knowledge page once you are done. You may also read tar’s manual on-line.

If you are employing a graphical LINUX operating system desktop, you may additionally use the file-compression utility or file manager enclosed together with your desktop to make or extract .tar files. On Windows, you may extract and build .tar archives with the free 7-Zip utility.

Related: How to use crontab in LINUX?

Employing the zip files

Zip is undoubtedly the most common compressed archive format around the globe. The Zip files generally end with ‘.zip’.

Compressing a Directory (Full of Files) into a ZIP File

Perform the subsequent command to put everything inside a specific directory into a compressed ZIP file.

zip -r FILE.zip DIRECTORY/

Explanation of the command flags 

-r: Recursively compresses all the files and directories, available in the ”DIRECTORY’ or in the zip file (otherwise you will only get the high-level files).

Uncompressing a ZIP file into the present directory

Execute the subsequent command for uncompressing the items in the ZIP file into the current or present directory.

unzip FILE.zip

Read More

Hey, we like you a lot and 

want to offer you some of the best content

Share your email for some exclusive insights