Compress command line linux

Archive, Compress, and Extract Files in Linux Using the Command Line

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

tar and gzip provide a standard interface to create archives and to compress files in Linux. These utilities take a large number of files, save them together in an archive, and compresses the archive to save space. tar does not compress files by itself. Used in conjunction with gzip , an archived file can be compressed to reduce disk space. The resulting archived file has the file extension, tar.gz and is sometimes called a “tarball”.

Archive a Directory

mkdir testdir && touch testdir/example.txt 
tar -cvf testdir.tar testdir/ 

Compression with gzip

total 9kB drwxrwxr-x 2 linode linode 5kB Jan 30 13:13 testdir -rw-rw-r-- 1 linode linode 1kB Jan 30 13:29 testdir.tar.gz

Extract a Tarball

The flags used in these example stand for:

  • -c : Create a new archive in the form of a tar file.
  • -v : Verbose flag, outputs a log after running the command.
  • -z : Zips or unzips using gzip .
  • -x : Extract a file from the archive.
  • -f : Define STDOUT as the filename, or uses the next parameter.

Common Options for Compressing and Archiving Files in Linux

Additional flags used with the tar command are:

Flag Function
-A Append tar files to an existing archive.
-d Show differences between an archive and a local filesystem.
-delete Delete from the archive.
-r Append files to the end of an archive.
-t List the contents of an archive.
-u Append but don’t overwrite the current archive.

These are the basics for working within the command line. Be sure to check the man pages man tar for a more detailed listing of possible flags when compressing and extracting files.

Читайте также:  Linux виртуальная файловая система

This page was originally published on Thursday, February 1, 2018.

Источник

13 Practical Examples of Using the Gzip Command in Linux

Compression is a very commonly performed operation by users to save disk space as well as reduce time and bandwidth while transferring large amounts of data over the network using gzip utility.

gzip stands for the GNU zip and it is a very popular compression and decompression utility. One of the primary reasons for its popularity is its high compression ratio and speed, which means the compressed data remains the same after decompressing.

The gzip command uses a deflate algorithm which is a lossless data compression that creates smaller file size to make file transfer much faster, as compared to other compression tools.

In this guide, we are going to discuss gzip command usage with examples in Linux.

gzip Command Syntax

Just like any other Linux command, the gzip command’s syntax is divided into two parts OPTIONS and FILES.

In the above syntax, the OPTIONS are used to alter the behavior of the command whereas the FILES represent the input files.

1. Create a Gzip File in Linux

One of the very common uses of the gzip command is to compress a large file. It is very common to see that, large ISO files or tar bundles are compressed to save disk space.

To compress a single file, we just need to pass the file name to the gzip command. To understand this, let’s use the following command to compress the ISO file:

Now, let’s use the ls command to verify that the file has been compressed:

Create Gzip File in Linux

In the above output, we can see the new compressed file with the name alma-linux.iso.gz. We can also observe that, by default, the gzip command adds a .gz extension to a compressed file.

2. Create Gzip and Keep Original File

In the previous example, we saw how easy it is to compress a file. However, if we observe carefully then we can notice that the gzip command deletes the original file after compressing it.

However, sometimes we want to keep the original file as well. In such cases, we can use the -k option as shown.

$ gzip -k alma-linux.iso $ ls -l

Gzip Keep Original File

In the above output, we can see that the original file is intact even after the compression.

3. View Contents of a .gz File

To view the contents of a compressed .gz file, use the zcat command – which allows you to view the compressed file contents without uncompressing.

To understand this, first, create a simple text file using the redirection operator:

$ echo "zcat example from tecmint.com" > demo $ gzip demo $ ls -l $ zcat demo

View Gzip Contents

In the above output, we can see that the zcat displays the contents of the compressed file without uncompressing it.

Читайте также:  Fglrx драйвер linux mint

4. View Info of a .gz File

Sometimes, we want to display more details about the compressed file. In such a case, we can use the -l option to list the following fields:

  • compressed size – it represents the size of the compressed file in bytes.
  • uncompressed size – it represents the size of the uncompressed file in bytes.
  • ratio – it represents the compression ratio.
  • uncompressed_name – it represents the name of the uncompressed file.

To understand this, let’s execute the following command:

View Gzip File Info

5. Overwrite Gzip File Without Confirmation

By default, the gzip command works in an interactive way if the compressed file with the same name already exists. To understand this default behaviour, let’s execute the same command from the previous example:

Gzip File Overwrite Confirmation

Here, we can see that the gzip command waits for the user input. Now, we can use the ‘y’ to overwrite the file or ‘n’ to abort the operation.

This interactive operation is safe and avoids overwriting files by mistake. However, this is not suitable every time. For example, if we are executing the gzip command from the script then the script will wait infinitely for the user input. In such cases, we can use the -f option which overwrites the files forcefully.

Now, let’s execute the same command with the -f option:

Gzip Force Overwrite File

Here, we can see that now the gzip command works in a non-interactive way.

6. Compress Multiple Files with Gzip

So far we saw how to compress a single file. In a similar way, we can use the gzip command to compress multiple files at once.

So, first, let’s create multiple copies of the file using the following cp command:

$ cp alma-linux.iso alma-linux-1.iso $ cp alma-linux.iso alma-linux-2.iso $ cp alma-linux.iso alma-linux-3.iso

Next, let’s compress the three files using the below command:

$ gzip alma-linux-1.iso alma-linux-2.iso alma-linux-3.iso $ ls -l

Create Multiple Gzip Files in Linux

7. Gzip All Files in a Directory

In the previous example, we saw how to compress multiple files. In a similar way, we can also compress all the files from a directory.

To understand this, first let us create a new directory and add a few files into it:

$ mkdir dir-1 $ touch dir-1/file-1.txt dir-1/file-2.txt dir-1/file-3.txt

Now, let’s use the -r option to compress all the files from the dir-1 directory:

Compress All Files in a Directory

In the above example, the -r option traverses the directory in a recursive fashion.

Читайте также:  Ubuntu kali linux repository

8. Decompress a Gzip File in Linux

The gzip command allows us to decompress the file using the -d option as shown.

$ gzip -d alma-linux.iso.gz $ ls -l

Unzip Gzip File in Linux

9. Compress Tar File to Gzip

In the previous two examples, we saw how to compress multiple files using a single command. However, we can also observe that the gzip command doesn’t compress these files into a single file. In such cases, first, we can create a tar bundle and then compress it using the gzip command.

So, first, let’s create a tar bundle with multiple files in it:

$ tar cvf sample.tar alma-linux-1.iso alma-linux-2.iso

Create Tar File in Linux

Now, let us compress this tar bundle using the following command:

Compress Tar File into Gzip

10. Speeding Up gzip Compression

So far, we used very small files to demonstrate the usage of the gzip command. However, in real scenarios, the data that needs to be compressed can be really large.

In such cases, we can use the —fast option to reduce the compression time.

It is important to note that, the —fast option gives preference to the compression speed over the ratio.

11. Speeding Up Gzip Compression Ratio

In a similar way, we can use the —best option to improve the compression ratio. To understand this, let’s execute the below command:

Here, we should note that the —best option gives preference to the compression ratio over the speed.

12. Set Gzip Compression Level

We can use an integer argument with the gzip command to regulate the speed of the compression. The valid value for the range is between 1 to 9. The value 1 represents the fasted compression whereas the value 9 represents the slowest compression.

For example, the following command uses the 2 as an argument to improve the compression speed:

It is important to note that, the default compression level in gzip is -6. It prefers high compression at the expense of speed.

13. Change Gzip File Extension Suffix

By default, the gzip command uses the .gz suffix after compressing the file. However, we can override this default behavior using the —suffix option.

For example, we can use the below command to use gnuzip as a suffix:

$ gzip --suffix .gnuzip alma-linux.iso $ ls -l

Change Gzip File Extension

In the above example, we can see the compressed file has a .gnuzip extension.

Conclusion

In this practical guide, we discussed some of the common examples of the gzip command in Linux to compress and decompress files faster.

Do you know of any other best example of the gzip command in Linux? Let us know your views in the comments below.

Источник

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