Compression – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Mon, 14 Dec 2020 05:21:19 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 How to Untar Files in Linux https://linuxhint.com/untar_files_linux/ Mon, 30 Nov 2020 15:45:17 +0000 https://linuxhint.com/?p=78723

Tar is quite a popular archive format, especially on Linux. In many cases, distros use tar archives to deliver package updates. Tar archives are also common to find when it comes to sharing files online.

Check out how to untar files in Linux.

Tar in Linux

For managing tar archives, all the Linux distros come with the tar tool. It’s this tool that we’ll use for extracting the contents of a tar archive.

Let’s demonstrate everything with an example. First, let’s create a tar archive with several files and directories. Here, I’ve created a directory with the name “Ants” that will be transformed into a tar archive.

$ tree Ants/

Now, let’s make a tar archive out of the directory. Here, tar will use various compression algorithms to do the job. It’s a common practice that the compression algorithm dictates the output file name.

To create a tar archive using gzip compression, use the following command.

$ tar -cvzf ants.tar.gz <source_file_directory>

To create a tar archive using bzip2 compression, use the following command.

$ tar -cvjf ants.tar.bz2 <source_file_directory>

To create a tar archive using XZ compression, use the following command.

$ tar -cvJf ants.tar.xz <source_file_directory>

Extracting tar files

List tar content

The following tar command will list all the files and directories included in the tar archive.

$ tar -tvf <tar_archive>

Let’s have a quick breakdown of the flags we used.

  • t: It tells tar to list the contents of the archive.
  • v: It tells tar to print its action on console.
  • f: It tells tar which file to perform the action on.

Extract entire file

Now, we’re ready to extract the tar archives we’ve got at hand. While you needed to use different commands to create different types of tar archives, we can use only a single tar command to extract all of them.

The following tar command will extract any valid tar archive. If files with similar filenames exist, upon extraction, tar will overwrite the files outside the archive.

$ tar -xvf <tar_archive>

Here, we’re facing one new tar flag.

  • x: It tells tar to extract an archive.

If you don’t want tar to overwrite existing data, add the “-k” flag. It tells tar not to overwrite/replace any existing file or directory.

$ tar -xvkf <tar_archive>

Extract specific files

There are some situations where you don’t need the entire tar archive extracted only to grab a single file. The tar tool offers such flexibility that you can extract only the select few files you need.

For this task, the tar command structure would look like this. Here, the file name would be the file name of your desired file. It must match with the file name that’s inside the tar archive.

$ tar -xvf <tar_archive> <filename>

If you want to extract a couple of files in such a manner, use the following command structure.

$ tar -xvf <tar_archive> <filename_1> <filename_2>

Extract specific directories

This is yet another awesome feature of the tar archive. Assuming the tar archive at your hand contains a directory or more, you can manually tell tar which directory to extract.

The command structure is similar to the section above.

$ tar -xvf <tar_archive> <directory>

If you want to extract multiple directories, then run the following command.

$ tar -xvf <tar_archive> <directory_1> <directory_2>

Final thoughts

Extracting tar archives is quite a simple task. All you need to know is the right tar command. If you’re interested in performing the actions with GUI and using a file manager, then your file manager should have the ability to extract tar archives by default.

In Linux, there are more tools to extract various formats of compressed archives. Check out how to extract compressed archives in Linux.

Happy computing!

]]>
Top 10 File Compression Utilities on Linux https://linuxhint.com/top_10_file_compression_utilities_on_linux/ Thu, 05 Nov 2020 08:13:57 +0000 https://linuxhint.com/?p=75453 Transferring files between one computer to another or storing them securely is a major task to both normal and professional users. Sometimes it is not possible to send files above a certain size over the internet, so you need utilities that will help decrease your file size without compromising data or its quality. It also helps merge multiple files and reduce the overall file size to help you send it securely over the internet.

Linux users are blessed with many effective and reliable file compression utilities at their disposal.

The majority of utilities listed here work well with all the Linux distros, and we have tested them on Ubuntu.

tar

The tar file compression is one of the most widely used file compression utilities on Linux. File compressed with this utility have suffix .tar.gz and .tgz, and they are also called tarballs.

For example, if we have a file/directory named swap1 in the current directory. To save it to a compressed file named file.tar,gz, we have to run the following command in terminal:

$ tar -czvf file.tar.gz swap1

You might be wondering what does exactly switch -czvf means, let’s see it one-by-one.

-c : Create an Archive
-z : Compress Archive with gzip
-v : known as “verbose”. It displays the progress in the terminal window when the archive is being created.
-f : This switch allows you to specify the file name of the archive.

Install it by running the following command in the terminal:

$ sudo apt-get install tar

gzip

The gzip stands for GNU Zip, and it is an open-source file compression format used to compress single files. It produces zipped files with the suffix .gz extension.

ZIP and GZIP, are both very popular file compression formats when it comes to saving space and reducing the time required to send the file over the internet.

Here is its basic syntax:

$ gzip [Options] [filenames]

Using switch -l will give you detailed information about the compressed file.

To unzip a file use syntax:

$ gzip -d filename.gz

To install gzip, run the following command in the terminal:

$ sudo apt-get install gzip

7zip

The 7zip is an open-source file compression utility that was initially developed for Windows users and was later ported to other operating systems like Linux and its distros. It supports multiple file compression formats and is popular for a high compression ratio with LZMA and LZMA2 compression techniques.

Syntax:

$ 7z a filename.7z filename

To extract:

$ 7z e filename.7z

To install 7zip, run the following command in the terminal:

$ sudo apt-get install p7zip-full p7zip-rar

lzma

The lzma is another file compression utility like zip or tar, and it ships-in pre-installed with Linux and its distros. It is quite a fast file compression utility as compared to others.

To create archive:

$ lzma -c --stdout filename> filename.lzma

To extract files:

$ lzma -d --stdout filename.lzma >filename

bzip2

The bzip2 is a free and open-source file compression utility. It is a faster file utility as compared to gzip but can only compress a single file at a time.
Syntax:

$ bzip2 filename

This technique uses more RAM during compression. To reduce its use, switch –s, as shown below:

$ bzip2 -s filename

To extract:

$ bzip2 -d filename.bz2

xz file compression

The xz is an upgrade to the lzma file compression utility but can only compress a single file at a time. It integrates well with all the Linux distros, even the older releases.

Syntax to compress:

$ xz filename

Syntax to extract:

$ xz -d filename.xz

shar

The shar, short for “shell archive”, is a simple and reliable file compression utility for personal and power users.

Syntax to compress:

$ shar filename > filename.shar

Syntax to extract:

$ unshar filename.shar

To install shar, run the following command in the terminal:

$ sudo apt-get install sharutils

ar

The ar is a widely used file compression utility in Debian and its derivatives.

Syntax to compress:

$ ar cvsr filename.a filename

Syntax to extract:

$ ar -xv filename

kgb

The kgb is free to use file compression utility with support for the majority of file formats. It supports archiving multiple files into one with a high compression ratio.

Syntax to archive:

$ kgp filename.kgb filename

Syntax to decompress:

$ kgb x filename.kgb

To install kgb, run the following command in the terminal:

$ sudo apt-get install kgb

pax

The pax stands for Portable Archive Exchange, and it is a file compression utility used in creating and extracting archives. It supports various file compression formats like tar, cpio, bcpio, and ustar.

Syntax to compress:

$ pax -wf filename.tar filename

Syntax to decompress:

$ pax -r  < filename.tar

To install pax, run the following command in the terminal:

$ sudo apt-get install pax

So, these are the top 10 file compression utilities for Linux and its distros. There are still other compression utilities for Linux, but these 10 are the standouts when tested on various parameters. Feel free to share your views with us at @linuxhint and @SwapTirthakar. ]]> How to Extract and Open a .gz File in Linux Command Line https://linuxhint.com/how_to_extract_and_open_a_gz_file_in_linux/ Tue, 27 Oct 2020 10:10:20 +0000 https://linuxhint.com/?p=73884

We know that a file in a computer system can be as small as a few Bytes or as large as a thousand Gigabytes. When you want to transmit a file from one end to another, its size plays a very important role in deciding whether you want to send it as it is or compress it. The .gz file format is a very commonly used compressed file format for the Linux operating system. Today, we will explore the methods of extracting and opening a .gz file in Linux.

Note: The flavor of Linux that has been used to demonstrate these methods is Linux Mint 20.

Methods of extracting and opening a .gz File in Linux Command

There are multiple different methods in which we can extract and open a .gz file in Linux. We have listed down all those methods below:

Method # 1: Using the “gzip” Command with “-d” Flag:

This method is helpful when you wish to extract the actual file while deleting the .gz file. For using the “gzip” command with the “-d” flag, you will have to perform the following steps:

For demonstrating the first three methods, we will first create a .gz file in our Home directory by running the following command in our terminal:

$ gzip FileName.txt

In our example, we already had a text file named gzFile.txt in our Home directory. We decided to create its .gz file with the above-mentioned command.


Once you run this command, you can visit your Home directory to verify if a .gz file has been created or not. Our .gz file is highlighted in the image shown below:


After creating this file, we will try to extract it by running the following command in our terminal:

$ gzip –d FileName.gz

Here, you can replace FileName with the name of your .gz file, which in our case was gzFile.txt.


When you will visit your Home directory after running this command, you will notice that your actual file has been extracted as highlighted in the image shown below. However, your .gz file has been removed because of running the above-mentioned command.

Method # 2: Using the “gzip” Command with “-dk” Flag:

This method is helpful when you want to extract the actual file while keeping the .gz file as well for future usage. For using the “gzip” command with the “-dk” flag, you will have to perform the following steps:

We will try to extract the same .gz file that we created above by running the command shown below:

$ gzip –dk FileName.gz

Here, you can replace FileName with the name of your .gz file, which, in our case, was gzFile.txt.


When you visit your Home directory after running this command, you will notice that your actual file has been extracted as highlighted in the image shown below. However, your .gz file has also been retained because of running the above-mentioned command.


Method # 3: Using the “gunzip” Command:

This method is an exact alternative to our Method # 1, which means that once you extract the actual file, the .gz file will not be kept any further. For using the “gunzip” command to extract and open a .gz file, you will have to perform the following steps:

We will try to extract the same .gz file that we created above by running the command shown below:

$ gunzip FileName.gz

Here, you can replace FileName with the name of your .gz file, which in our case was gzFile.txt.


In our case, since we already had an extracted file with the same name in our Home directory because of running the command shown in Method # 2, our terminal prompted us if we want to overwrite this file or not, therefore, we proceed by entering a “y” as shown in the following image. However, if you have not performed any other extraction methods on the same file before, then running this command will not display any such message.


When you visit your Home directory after running this command, you will notice that your actual file has been extracted as highlighted in the image shown below. However, your .gz file has been removed because of running the above-mentioned command.


Once you have your .gz file extracted by following any of the three methods shown above, you can open it simply by double-clicking on it.

Method # 4: Using the “tar” Command:

At times, instead of having a simple .gz file, you have a .tar.gz or a .tgz file, which can be extracted and opened with the help of the “tar” command in the following manner:

For demonstrating this method, we will first create a .tgz file in our Home directory by running the following command in our terminal:

$ tar –czvf NameOftgzFile.tgz NameOfActualFile.txt

In our example, we already had a text file named targzFile.txt in our Home directory. We decided to create its .tgz file with the above-mentioned command.


When you run this command, the name of your actual file will appear on the terminal, which will indicate that its .tgz file has been created as shown in the image below:


Once you run this command, you can also visit your Home directory to verify if a .tgz file has been created or not. Our .tgz file is highlighted in the image shown below:


After creating this file, we will try to extract it by running the following command in our terminal:

$ tar –xf FileName.tgz

Here, you can replace FileName with the name of your .tgz file, which in our case was targzFile.txt.


When you visit your Home directory after running this command, you will notice that your actual file has been extracted as highlighted in the image shown below. However, your .tgz file has also been retained because of running the above-mentioned command.


Once you have your .tgz file extracted, you can open it simply by double-clicking on it.

Conclusion:

The four methods discussed in this article provide you with great solutions for extracting and opening the .gz as well as the .tgz files in Linux. You can choose to follow any of these methods according to your liking.

]]>
How to Extract Compressed Archive Files in Linux https://linuxhint.com/extract_compressed_archive/ Fri, 29 May 2020 07:11:05 +0000 https://linuxhint.com/?p=60515 This article will list command line methods to extract various compressed archive file formats in Linux. These archives can include single or multiple files and folders, usually compressed to save disk space. Archive file formats are also used for splitting humongous files that can run into GBs. They also make file operations much easier as you must handle a single file or small number of split files only instead of handling many scattered files. Many algorithms for bundling, archiving, and compressing files exist, and this article will explain command line methods to decompress archives made using such algorithms.

GUI Method (Stock Ubuntu with GNOME SHELL)

Ubuntu comes with decompression support for limited compressed archive file formats – mainly zip files, tar archives, and 7z files. To enable full support for other major archive formats, you must install a few extra apps. Run the command below to do so:

$ sudo apt install zip unzip rar unrar p7zip-full

Now you can double click or right click on any compressed archive in the Nautilus file manager to extract files.

The archive manager in Nautilus file manager also includes support for extracting password protected files, so you don’t need any extra apps to extract encrypted archives.

File managers in other desktop environments also have support for extracting archives via plugin systems. You must either install an app named “archive manager” or “file-roller” to add support for extracting files from archives.

7z

7z is one of the most widely used compressed archive file formats. It can deep compress files at a much better ratio than other popular archive formats like zip and rar.

To enable 7z archive support on Ubuntu, run the command below:

$ sudo apt install p7zip-full

To extract a 7z archive to a new directory named same as the archive name, run the command below:

$ 7z x archive.7z

The command above can also be used for password protected 7z archives. You will be prompted to enter a password after running the command above.

Zip

Zip archive is another most widely used file format used for archiving and compression purposes. It is the primary archive file format supported on Windows and ships by default on all major versions of Windows.

To enable zip archive support on Ubuntu, run the command below:

$ sudo apt install zip unzip

To extract a zip archive to a new directory named same as the archive name, run the command below:

$ unzip archive.zip

Like 7z command, unzip command can also be used to extract password protected files. Users will be prompted to enter the password after running the command mentioned above.

Rar

Rar file format is a proprietary archiving and compression format. The support for rar files on Linux is not as robust as other archive file formats but is enough for doing simple compression and decompression tasks.

To add rar archive support on your Ubuntu installation, run the command below:

$ sudo apt install rar unrar

To decompress a regular or password protected rar archive, run the command below:

$ unrar x archive.rar

Tar, Tar.gz, Tar.xz, Tar.bz2, Tgz, Gz

Tar archive file format is available by default on almost all Linux and Unix based operating systems. By default, tar archives are not compressed, you must use additional compression algorithms like gz, bz2 and so on. To extract a tar file, run the command below:

$ tar xf archive.tar

You can replace “archive.tar” in the command above with the correct archive name and file extension. Tar archives don’t have built-in support for encryption, so you can’t use password protection.

Conclusion

These are various command line utilities to extract archive file formats. The tar file format is widely used for distributing software and large files on Linux, though it natively doesn’t support password protection. Other file formats, like zip, rar, and 7z, do support password protection, but they are easier to crack than other encryption methods like GPG (GNU Privacy Guard).

]]>
How to view contents of ZIP archive in Linux https://linuxhint.com/view_zip_archive_contents_linux/ Mon, 09 Mar 2020 19:05:27 +0000 https://linuxhint.com/?p=56305 You most probably have used compression and archiving techniques for backup or for saving space on your hard disk. To view and access the contents of those archives, you have to extract them in a directory, which seems a pretty easy task. However, if you are dealing with the archive containing a large number of files or the files with big size, then it will become a time-consuming and annoying task. There are some commands in Linux that allows you to quickly view the contents of an archive without extracting it.

In this article, we will see some of the Linux commands that will help us to view the contents of an archive without the need to extract them.

Please note that we have explained the procedure and commands described in this article on a Debian 10 system.

Viewing the contents of ZIP archive

Using zmore and zless

Similar to more and less command in Linux, these commands can be used to view the contents of a file from the command line without extracting. Zmore and Zless command works perfectly for a ZIP file; however, these do not work for a ZIP folder that contains the multiple files.

To view the contents of a ZIP file without extracting, simply use zmore or zless command followed by the filename:

$ zmore <archive_name>

or

$ zless <archive_name>

Using zcat

Similar to zmore and zless commands, zcat can also be used to view the contents of a ZIP archive without extracting it. To view a compressed file, use zcat followed by the file name:

$ zcat <archive_name>

It also does not work with ZIP folders containing multiple files. If you run zcat to view a ZIP archive that contains the multiple files, it will only show one file ignoring the rest of the files, as shown in the following screenshot.

Using Vim

Vim command can also be used to view the contents of a ZIP archive without extracting it. It can work for both the archived files and folders. Along with ZIP, it can work with other extensions as well, such as tar.xz, tar.bz2, tar, tbz.

To view a compressed file, use zcat followed by the filename:

$ vim <archive_name>

It allows us to browse through the list of files in an archived folder and view the contents of a specific file as well. In order to view a specific file, use the arrow and Enter keys to select the file and press Enter or use the left click to open the specific file.

In order to view a specific file, select it using the arrow keys and then hit Enter

Using zip and unzip command

Zip is the most common method of archiving the files, while the unzip helps to extract those files. When unzip command is used without any flag; it extracts all the files contained in a ZIP archive. However, we can use it to view the contents of a file without extracting it by using a specific flag. Similarly, the zip command can also be used to view the list of files in an archive without decompressing it. However, it cannot be used for viewing the contents of a file.

To use zip and unzip, you will first need to install them in your system. To do so, execute this command in Terminal:

$ sudo ap-get install zip unzip

In order to use the zip command for browsing the list of files inside a ZIP archive without decompressing, type zip followed by –sf and the archive name as follows:

$ zip –sf <archive_name>

The unzip command allows you to view the contents of a file along with browsing the list of files. It works for both the ZIP archived files and folders.

To browse the list of files in an archived folder, use unzip with –l flag as follows:

$ unzip –l <archive_name>

To view the contents of all the files, use unzip with -c flag as follows:

$ unzip –c <archive_name>

In order to view the contents of a specific file in the archive folder, add the filename at the end of the above command as follows:

$ unzip –c < archive_name> filename

Using 7z

7z is another useful tool used for archiving and extracting the files. It supports various extensions, including ZIP, 7Z, XZ, TAR, WIM, etc. It can also be used for viewing the list of files in an archive without extracting them. However, it does not support displaying the contents of files in an un-extracted format.

To install the 7z utility, execute this command in Terminal:

$ sudo apt install p7zip-full

Once installed, you can view the contents of a ZIP archive using the l flag as follows:

$ 7z l <archive_name>

In this article, we have discussed some Linux commands for viewing the contents of an archive file. By using these commands, you will no longer need to extract the heavy archive files for just viewing their content.

]]>
Linux File Compression Options and Comparison https://linuxhint.com/linux_file_compression/ Tue, 28 Jan 2020 15:48:57 +0000 https://linuxhint.com/?p=54201 Compression, in general, is a useful method that is essentially encoding information using less data than the original one. In the case of Linux, there are various compression options, each with its own benefits.

A generic Linux distro offers access to a handful of really useful and simple compression mechanisms. This article will only focus on them.

Compression types

Compression is encoding and representing information using fewer bits than it originally was. In the case of file compression, a compression method utilizes its own algorithm and mathematical calculation to generate an output that’s generally less than the size of the original file. Because of how different compression works and the random nature of files, the mileage may vary greatly.

There are 2 types of compression.

  • Lossy compression: This is a risky type of compression that doesn’t guarantee data integrity. Essentially, once compressed, there’s a risk that the original file can’t be reconstructed using the compressed archive.
    A solid example of this type of compression is the well-known MP3 format. When an MP3 is created from the original audio file, it’s significantly smaller than the original source music file. This causes loss of some audio quality.
  • Lossless compression: This is the most widely used type of compression. Using a “lossless” compression method, the original file can be reconstructed from the compressed file. The compression methods I’ll discuss in this article are all lossless compression methods.

Linux compression

Majority of the compression methods are available from the tool tar. As for the “zip” compression, we’ll be using the zip tool. Assuming that your system already has these tools installed, let’s get started.

At first, we need a test file. Run the following command to create one.

$ base64 /dev/urandom | head -c 20000000 > file.txt

It’ll create a text file with 20MB size.

Now, let’s create 10 copies of the file. Together, it’s 200 MB.

Zip For Compression

Zip is quite common. For creating a zip file, the zip tool requires the following command structure.

$ zip <output>.zip <input>

To compress all the files under the test directory in a single zip file, run this command.

$ zip test.zip *

The input size was 200 MB. After compression, it’s now 152 MB!

By default, the zip tool will apply the DEFLATE compression. However, it’s also capable of using bzip2 compression. Not only that, you can also create password-protected zip files! Learn more about zip.

Tar for Compression on Linux

Tar isn’t a compression method. Instead, it’s most often used for creating archives. However, it can implement a number of popular compression methods to the archive.

For handling tar (also known as “tarball”) archive, there’s the tar tool. Learn more about tar. Generally, the tar tool uses the following command structure.

$ tar <options> <output_file> <input>

To add the test files into a single tar archive, run the following command.

$ tar -cvf test.tar *

Here, the file size remains the same.

Gzip for Compression on Linux

GNU Zip or gzip is another popular compression method that, in my opinion, is better than the traditional zip because of its better compression. It’s an open-source product created by Mark Adler and Jean-Loup Gailly that was originally destined to replace the UNIX compress utility.

For managing gzip archives, there are 2 tools available: tar and gzip. Let’s check out both of them.

First, the gzip tool. Here’s how the gzip command structure looks.

$ gzip <option> <input>

For example, the following command will replace test1.txt with test1.txt.gz compressed file.

$ gzip -v test1.txt

If you want to compress an entire directory using gzip, run this command. Here, the “-r” flag is for “recursive” compression. Gzip will go through all the folders and compress the individual file(s) in each of them.

$ gzip -r <folder_path>

Gzip supports various compression strength value, starting from 1 (least compression, fastest) to 9 (best compression, slowest).

$ gzip -v -9 <file>

For better control over the output and ease-of-use, tar is better for the task. Run the following command.

$ tar -cvzf test.tar.gz *

The result is similar to zip using DEFLATE, resulting in 152 MB after compression.

Bzip2 for Compression on Linux

Bzip2 is a free and open-source tool that uses the Burrows-Wheeler algorithm for compression. First introduced back in 1996, bzip2 is heavily used as an alternative to the gzip compression.

Like gzip, there are 2 tools to work with bzip2: tar and bzip2.

The bzip2 tool works similar to the gzip tool. It can only work with just a single file at a time. Here’s the command structure.

$ bzip2 <option> <input>

Let’s compress the test1.txt file. Here, the “-v” flag is for verbose mode.

$ bzip2 -v test1.txt

Similar to gzip, bzip2 also supports different level of compression, starting from 1 (default, less memory usage) to 9 (extreme compression, high memory usage).

$ bzip2 -v -9 <file>

The better way of using bzip2 compression is by using tar. Use the following command.

$ tar -cvjf test.tar.bz2 *

The compression is slightly improved than the previous ones. Now, the file size has shrunk to 151.7 MB.

XZ for Compression on Linux

It’s a relative newcomer in the field of compression. First released in 2009, it has seen a steady growth of usage since then.

The xz compression tool uses the LZMA2 algorithm that’s known for greater compression ratio compared to gzip and bzip2, making it a great choice when you want to save the maximum amount of disk space. However, this comes with the cost of higher memory requirements and time consumption.

File created by the XZ compression tool has the extension .xz. For compressing a single file, you can directly call the XZ tool.

$ xz <option> <file>

For example, run the following command to compress the test1.txt file.

$ xz -v test1.txt

Similar to other compression methods mentioned, xz also supports various range of compression strength, starting from 1 (lowest compression, fastest) to 9 (best compression, slowest). If you don’t have any regard for time and just want to save space, then go for the extreme.

$ xz -v -9 <file>

To create a compressed XZ file from all the test files, run this command.

$ tar -cvJf test.tar.xz *

Here, the output file size is 153.7 MB.

Extracting compressed archives

Extracting the archives we created is easier than creating them. To extract a zip file, use the following command structure.

$ unzip <filename>.zip -d <destination>

To extract the zip archive we created, run this command. This will extract all the contents in the same directory.

$ unzip test.zip

For extracting tar, tar.gz, tar.bz2 and tar.xz archives, we have to use the tar tool. The following tar command is applicable for extracting all of them.

$ tar -xvf <archive_filename>

For example, let’s extract all the files from the bz2 compressed archive.

$ tar -xvf test.tar.bz2

To decompress a gzip (not tar.gz) file, run this command.

$ gzip -d <gzip_file>

Similarly, the following command will decompress bzip2 archive.

$ bzip2 -d <bzip2_file>

Same command structure applies for xz archive.

$ xz -d <xz_file>

Final thoughts

Hopefully, now you have enough knowledge to handle the compression tasks in different circumstances. Depending on the specific requirement, all the compression methods offer very attractive features.

One important thing to note is, the compression result won’t be the same all the time. With different data input, the output will be different. For example, in some cases, xz can offer insane compression result whereas in this example, it didn’t. Same goes for other methods.

To learn more in-depth about these tools, check out their respective man page.

$ man zip
]]>
How to Create and Extract TAR.GZ Files on Ubuntu 18.04 https://linuxhint.com/create_extract_tar_gz_ubuntu/ Sun, 26 Aug 2018 18:40:45 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=29682 If you’re a Linux user, you already know the power of it, right? You can perform numerous actions – creating and extracting archives, fine-tuning the system look and even how the system works!

Did you ever have to work with TAR.GZ files? TAR.GZ is a special type of archive that is being handled with the help of “tar” tool. TAR.GZ is a widely used archive type in the Linux community. For example, you’ll find plenty of software that comes up in TAR.GZ archive. The compression ratio is also quite good and doesn’t require any 3rd-party tool to handle it. Tar is a built-in tool on all the major Linux distros including Ubuntu 18.04.

Today, let’s get familiar with the TAR.GZ file on Ubuntu 18.04.

There are a number of other tools to check out! Take a look at the 100 best Ubuntu apps for you.

Tar short intro

Before jumping deep into the TAR.GZ, let’s become familiar with the “tar” tool. Tar is a part of the GNU software collection. Fun fact – tar is also a part of Windows (Windows 10)! Finally, Microsoft understands the importance of the tool!

Tar follows the following structure –

tar [option…] [file…]

In the option parameter, there will go all the operation choices. Tar supports numerous operations. You can find them out if you run the following command on the terminal –

tar --help

In the file parameter, you can provide file or directory path. If the file(s) isn’t in the current directory, you can also specify the entire path to the file. Tar allows multiple files and/or directories in the parameter.

Creating TAR.GZ file

Let’s get into the main part. For this guide, I’ve already created a test directory. Its location – /home/Viktor/Desktop/testDir.

Creating a TAR.GZ file is pretty simple. Run the following command –

cd ~/Desktop/
tar -czvf test.tar.gz test1.txt test2.txt test3.txt

For including all the files inside, run this command –

tar -czvf test.tar.gz *

Let’s explain the entire command. This command line follows the following structure –

tar [option…] [output file] [source file/directory…]

As of the options,

  • c – Telling “tar” tool to create an archive
  • z – Use “gunzip” compression. That’s why the file extension – GZ.
  • v – Verbose mode. Tar will log each of its activity into the screen.
  • f – Allows you specifying a file name for the output file.

If you wanted to create an archive from a directory, run the following command –

tar -czvf ~/Desktop/test.tar.gz ~/Desktop/testDir/

In cases, you may need to compress an entire directory but exclude some specific file/directory. Then, your command would be something like this –

tar -czvf ~/Desktop/test.tar.gz * --exclude=*.txt

The “exclude” switch is extremely powerful. It doesn’t take files; instead, it accepts patterns.

Extracting TAR.GZ

We’ve learnt creating TAR.GZ files but without the knowledge of extracting it, everything is in vain. Run the following command to extract the TAR.GZ file –

cd ~/Desktop/testDir/
tar -xzvf test.tar.gz

Here, all the options are just the same as before. The only difference is the “x”.

  • x – Tells “tar” to extract an archive.

Note that the extraction procedure will replace any file that matches the file name of the archive.

Need to extract the archive in somewhere else? Run the following command –

tar -xzvf test.tar.gz -C ~/Desktop/testDir1/

Voila! Enjoy playing with TAR.GZ!

]]>
How to Use CentOS Unzip https://linuxhint.com/centos_unzip/ Thu, 16 Aug 2018 07:35:04 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=29455 Zip is a file compression utility. It is the default file compression utility on Windows and widely used in Windows operating system. The Zip compressed files are also called zip files. The best thing about Zip is that it is cross platform. Zip files can be opened in Mac OS, Windows and Linux. You can also create Zip files from Mac OS, Windows, and Linux and open it in other operating systems.

On Linux based operating systems such as CentOS, the unzip command is used to open Zip files.

In this article I will show you how to use the unzip command to open Zip files on CentOS. I am going to use CentOS 7 for the demonstration. Let’s get started.

Installing unzip on CentOS 7:

On CentOS 7, the unzip package may not be installed by default. But it is available in the official package repository of CentOS 7. So it is very easy to install.

First update the YUM package repository cache with the following command:

$ sudo yum makecache

The YUM package repository cache should be updated.

Now install unzip with the following command:

$ sudo yum install unzip

Now press y and then press <Enter> to continue.

unzip should be installed.

Now to check whether unzip works, run the following command:

$ unzip -v

As you can see, unzip is working perfectly.

Working with Zip Files Using Graphical User Interface:

If you have graphical desktop environment such as GNOME desktop environment installed on your CentOS operating system, then working with Zip files is pretty easy.

As you can see, I have app.zip file in the ~/Downloads directory of my HOME directory.

Now if you right click on the zip file, you should be able to see the following options. The first option is Open With Archive Manager. If you double click on a Zip file, it will open with the Archive Manager by default.

You can also click on Extract Here to extract the zip file.

As you can see, the zip file is opened in the Archive Manager and I can see all the contents of the Zip file.

From the Archive Manager, you can click on Extract to extract the contents of the Zip file.

You should see the following window. You can click on the Extract button to extract the Zip file in the current directory. If you wish, you can create a new folder and extract the contents of the Zip file there.

I create a new directory myapp/ and extracted the contents of the Zip file there.

As you can see, the contents of the Zip file are correctly extracted into the myapp/ directory.

You can also select specific files and folders from the Archive Manager and drag and drop them on a directory. Only your desired files and folders will be extracted.

Listing the Contents of the Zip file using unzip Command:

You can list all the files and directories stored inside a Zip file with the following command:

$ unzip -l app.zip

As you can see, all the contents of the Zip archive app.zip is listed.

Extracting a Zip File using unzip Command:

You can extract a Zip file using the unzip command very easily.

To extract a Zip file into the current directory where the zip file is, run the following command:

$ unzip app.zip

NOTE: Here app.zip is the Zip file that I am extracting.

The contents of the Zip file should be extracted to your current working directory.

As you can see, all the files and directories of app.zip is extracted.

In the earlier example, all the files were extracted in the current directory. You may want to extract the contents of the Zip file to any specific directory. You can also do that with the unzip command.

First create the directory where you will be extracting the Zip file with the following command:

$ mkdir ~/Downloads/myapp

Now run the following command to extract the Zip file app.zip into the

~/Downloads/myapp directory:
$ unzip app.zip -d ~/Downloads/myapp

The contents of the app.zip file is extracted into the ~/Downloads/myapp directory as you can see in the screenshot below.

The ls command also verifies that the Zip file was extracted in the desired directory.

Extracting Specific Files from the Zip File using unzip Command:

You can also extract specific files and directories from the Zip file using unzip command.

For example, let’s say you want to extract only server.js file from app.zip file, run the following command:

$ unzip app.zip server.js

server.js should be extracted.

As you can see, only server.js was extracted to the current working directory.

You can also specify a directory where the files and directories should be extracted as follows:

$ unzip app.zip server.js -d ~/Downloads/myapp

You can also extract a specific subdirectory from the Zip file as follows:

$ unzip app.zip 'models/*' -d ~/Downloads/mypp

 

As you can see the directory models/ and all the contents of the directory was extracted into the ~/Downloads/myapp directory.

That’s how you use Unzip on CentOS. Thanks for reading this article.

]]>
XZ Compression Tutorial https://linuxhint.com/xz_compression_tutorial/ Thu, 07 Jun 2018 08:00:35 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=27102 Many built-in command-line compression tools are available in Linux operating system. XZ is one of the popular data compression tools that works like any other standard compression tools, such as gzip, bzip2 etc. XZ command compresses and decompresses the file which is mentioned in the command with XZ. If any file name is not mentioned in the XZ command then the command reads data from standard input and after processing the data, write the data in standard output. XZ can compress or decompress one or more files at a time. Normally it removes the original file after compression and removes decompressed file after decompression. You can keep both compressed and uncompressed files by using some options. How you can use XZ command for compressing and decompressing file is shown in this tutorial.

Run the following command to find out the options of XZ command.

$ xz --help

XZ Compression Tutorial

Select some files that you want to test xz command. Here, the files of myDir folder are used for compressing and decompressing with xz command. Run ls -l command to show the list of files with detail information.

$ ls -l

Compress single file

You can compress any simple file by giving the file name with xz command. After compression, xz command deletes the original file. Here, b1.sh file is compressed and created the compressed file named b1.sh.xz.

$ xz b1.sh
$ ls -l

Compress multiple files

You can compress multiple files using xz command by separating each file with space. Here, two compressed files, loop2.sh.xz and myfile.txt.xz are created after deleting loop2.sh and myfile.txt.

$ xz loop2.sh myfile.txt
$ ls -l

Compress file by keeping original file

If you want to keep original file with compress file then run xz command with –k option. Here, comment2.sh.xz file is created without removing comment3.sh file.

$ xz -k comment3.sh
$ ls -l

Decompress file

Like compression, when you decompress any file with xz command then it delete decompressed file after

decompression.  Here, b1.sh file is created after decompressing b1.sh.xz file. –d option is used with xz command to decompress any compressed file.

$ xz -d b1.sh.xz
$ ls -l

Decompress file by keeping compressed file

You have to use –k option with unxz command to keep the decompressed file with original file after decompression. After running the following command, loop2.sh.xz file will not be removed.

$ unxz -k loop2.sh.xz
$ ls -l

Compress multiple file in a single file

If you want to create single compression file for multiple files then you need to use –c option. The following command will create new.xz compressed file after compressing b1.sh and FirstJava.class files.

$ xz -c b1.sh FirstJava.class > new.xz
$ ls -l

Retrieve information of compressed file

You can retrieve information of any compressed file by using –l option. Here, new.xz file is created by compressing two files. The following information shows that the original size of both files is 604 bytes and after compression the size is 548 bytes.  You can decompress this file by using previous decompression options.

$ xz -l new.xz

One of the major limitation of xz compression tool is that it can compress file only. You can’t compress any folder by using xz. This tool can’t also be used for creating password protected compressed file. There are other tools on Linux to create password protected compressed file. One of them is rar package. You can easily install trial version of rar package on Linux to create and open archive files. You can read the tutorial on rar package from the following link.

]]>
RAR Archive Ubuntu https://linuxhint.com/rar_archive_ubuntu/ Mon, 04 Jun 2018 03:02:48 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=27063

How to create and open an archive with RAR on Ubuntu

To compress, archive and transfer large files or folders, rar package can be used on Ubuntu. Rar is a common archiving software to compress large files or folders. Suppose, if you want to transfer a folder from one location to another location which contains many files and folders, then the first option is to compress the folder and create an archive file. The compress file can be easily attached with email or transferred to other location. Many options are available on Ubuntu to compress files or folders. But if the files and folders are compressed using rar command then the created rar file can also be extracted on windows. Because rar files are also supported by windows. How you can install and use rar package to compress or decompress files and folder is shown in this tutorial.

Installing rar package

Rar package is not installed on Ubuntu by default. So, run the following command to install rar on Ubuntu.

$ sudo apt-get install rar

Rar package has various commands for creating archive files and folder. To get the list of rar commands just run rar without any command.

$ rar

The following output shows that trial version of RAR 5.40 is installed on the system. You can use any command from the list with rar for the particular purpose. Use of some basic commands of rar are shown in this tutorial.

Creating archive file

Select any large file or folder to compress and create archive file using rar command. In the following example, code folder is selected to create the archive file. code folder contains 7 files and the size of code folder is 4096 bytes.

To add the file into archive, a command is used with rar and next type the name of rar file and the name of the file or folder that you want to compress. Run the command to create the archive file, code.rar file by compressing code folder.

$ rar a code.rar code

After compression, the size of code.rar file is 1600 bytes. Run the following command to check the size of the code folder and the archive file.

$ ls  -la

Showing the content of archive file

Rar uses l command to show files and folder list of archive file. Run the following command to show the content of code.rar file.

$ rar l code.rar

Extracting archive file 

Move code.rar folder in different location. Suppose, the archive file is moved in mydir folder. x command is used with rar to extract the archive file. Run the following command to extract the file in the current location. When code.rar file will extract then code folder will be created after extraction.

$ rar x code.rar

Now, run ls -la command to check the folder is created or not after extraction.

$ ls -la

Creating and extracting password protected archive file

To create password protected archive, rar uses -p option with a command. After running the command, you have to set the same password for two times to create password protected archive file. The following command will create password protected archive file, pcode.rar of the folder code.

$ rar a -p pcode.rar code

Now, if you want to extract pcode.rar file then it will ask for the password which is set during the archive file creation. After setting the password, type a to use current password for all files and folders of the archive file.

$ rar x pcode.rar

The above command shows the mostly used commands of rar to create and extract any archive file. There are many other uses of rar, such as, recovering archive, updating archive, locking archive, setting archive comments, deleting archive etc. ]]> How to Zip a Folder in Linux https://linuxhint.com/zip_folder_linux/ Mon, 21 May 2018 17:14:31 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=26609 In this article, I will show you how to Zip and Unzip a Folder or Directory in Linux.  This might seem trivial, but sometimes people will struggle to get it right. Let’s get started.

Installing Zip Utilities on Ubuntu/Debian

First update the apt package repository cache with the following command:

$ sudo apt-get update

The apt package repository cache should be updated.

Now install zip and unzip packages with the following command:

$ sudo apt-get install zip unzip -y

Zip and unzip packages should be installed. In my case, they are already installed.

Installing Zip Utilities on RHEL 7/CentOS 7

First update the yum package repository cache with the following command:

$ sudo yum makecache

Now install the zip and unzip packages with the following command:

$ sudo yum install zip unzip

Now press y and then press <Enter> to continue.

zip and unzip packages should be installed.

Zipping a Folder/Directory Graphically

If you have any graphical desktop environment installed on your chosen Linux distribution, then you can use it to Zip archive any folder you like very easily.

First open your favorite File Manager and go to the location where you have the folder you want to Zip archive. In my case I am using the Nautilus file manager in GNOME 3 desktop environment.

Let’s say you want to Zip archive the Downloads/ directory as marked in the screenshot below.

Now right click on the Downloads/ directory and click on Compress… as marked in the screenshot below.

Now type in a name for your Zip archive and select .zip

Once you’re done, click on Create.

A backup.zip file should be created. This is the Zip archive of the Downloads/ directory.

Zipping a Folder/Directory using the Command Line Interface (CLI)

If you don’t have any graphical desktop environment installed on your computer, don’t worry. You can still use the command line interface (CLI) to Zip archive a folder.

First go to the location where the folder you want to zip archive is available with the following command:

$ cd PATH

NOTE: PATH is the location where your desired folder is located.

For example, if you want to Zip archive the /etc directory. So the PATH should be the root directory /.

Again, if you want to Zip archive the /etc/apt directory, then the PATH should be /etc.

Let’s Zip archive, /etc/apt directory.

$ cd /etc

The command for Zipping a folder or directory is:

$ zip -r OUTPUT.zip FOLDER

NOTE: Here FOLDER is the directory that you want to Zip archive. OUTPUT is the path to a file where the Zip archive of FOLDER will be saved.

For example, run the following command to Zip archive /etc/apt directory and save it to the HOME directory of your login user as apt_backup.zip:

$ zip -r ~/apt_backup.zip apt/
Or
$ zip -r $HOME/apt_backup.zip apt/

Zip Folder Linux

The /etc/apt directory or folder should be Zip archived.

It should be save to ~/apt_backup.zip file as you can see from the screenshot below.

$ ls -lh ~
Or
$ ls -lh $HOME

Extracting the Zip Archive Graphically

If you have a graphical desktop environment installed, then extracting the Zip archive is very easy.

Just right click on the Zip archive you want to extract and you should see the following menu. Select either Extract Here or Extract to… to unzip it.

If you want to extract the archive to your current working directory (the directory you’re in right now), then click on Extract Here. It should be extracted as you can see from the marked section of the screenshot below.

If you want to extract it to a different directory, then click on Extract to…

A directory picker should be opened as you can see in the marked section of the screenshot below.

Select a directory and click on Select.

The Zip archive should be extracted in that directory as you can see in the marked section of the screenshot below.

Extracting the Zip Archive using the Command Line Interface (CLI)

If you don’t have the graphical desktop environment installed on your Linux distribution, don’t worry. You can extract the Zip archive using the command line interface (CLI).

First navigate to the directory where you want to extract the Zip archive with the following command:

$ cd EXTRACT_DIR

NOTE: EXTRACT_DIR is the directory where you want to extract the Zip archive.

Then run the following command to extract the Zip archive:

$ unzip ZIP_ARCHIVE.zip

NOTE: Here ZIP_ARCHIVE is the path to the Zip archive that you want to extract.

For example, let’s extract the ~/apt_backup.zip file to ~/Downloads/ directory.

First navigate to the ~/Downloads directory:

$ cd ~/Downloads

Now run the following command to extract apt_backup.zip file:

$ unzip ~/apt_backup.zip

~/apt_backup.zip file should be extracted.

The extracted apt/ directory.

That’s how you Zip and Unzip a Folder or Directory in Linux. Thanks for reading this article.

]]>
Install 7Zip Compression Tool on Ubuntu https://linuxhint.com/install-7zip-compression-tool-on-ubuntu/ Thu, 22 Feb 2018 06:17:19 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=22729

How to Install 7-zip on Ubuntu and use it to compress and decompress any file or folder

When you want to transfer large size of files or folder from one location to another then you need to use any good compression tools for making the task easier.  Many free tools are available to compress and decompress files and folders. One of them is 7-zip file archiver. It is an award-wining open-source file archiver with high compress ratio. This software supports most of the popular operating systems and multiple file formats. You can use this to compress and decompress files and folders in 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM formats. Some important features of this tool are mentioned below.

  • It provides better compression ratio which is 2-10% better than other similar tools.
  • It uses strong AES-256 encryption.
  • It has self-extracting capability for 7z file format.
  • It supports 87 languages

In this tutorial, you will learn how you can install and use 7-zip file archiver on Ubuntu operating system.

7-zip Installation:

Update your operating system before running the command to install 7-zip file archiver.

$ sudo apt-get update

p7zip package contains 7-zip. Two types of p7zip packages are available. These are p7zip and p7zip-full. If you want to make auto extractable archive also with other archive options then install p7zip-full. The command for p7zip-full package is executed here.

$ sudo apt-get install p7zip-full

Check the package is properly installed or not by using 7z command.

$ 7z

p7zip 16.02 version is installed here which contains 7-zip 16.02.

Using 7-zip

Select any file or folder to make compress file by using 7-zip. Type ls -la command to show the list of all files and folders of the current directory. Here, data.txt file is selected for compression. The size of the file is 540 bytes.

$ ls -la

Run the command to create compressed file named data.7z. The option a is for archive or compress.  After compression, the size of archive file is 152 bytes.

$ 7z a data.7z data.txt

To show the detail information list of any archive file, run 7z command with l option.

$ 7z l data.7z

Run 7z command with e option to decompress or extract the files and folder from a archive file.

$ 7z e data.7z

By using above steps you can create archive file of any folder also. You can create archive file of files and folders without using commands or terminal. Go to file location, select the file and right click on it to open pop-up menu. Click compress option from menu to create archive file.

Three archive options are available in the dialog box which are .zip, .tar.xz, .7z. Here, .zip is selected to create a compressed file named myfile.zip.

7-zip archiver is an efficient tool to compress and decompress your necessary files and folder.  You can easily transfer large amount of data by using this archiver.

 

]]>