قالب وردپرس درنا توس
Home / Tips and Tricks / How to Compress or Decompress Files from the Linux Terminal

How to Compress or Decompress Files from the Linux Terminal



  Linux Shell Prompt

ZIP files are universal archives commonly used on Windows, MacOS, and even Linux systems. You can use some common Linux terminal commands to create a ZIP archive or extract files from it.

The ZIP-compressed archive file format

ZIP files are probably the most used form of compressed archive in the world because of the dominance of the ZIP format in the Windows domain.

While .tar.gz and tar.bz2 files are common on Linux, Windows users are likely to send you an archive in ZIP format. If you want to archive some files and send them to a Windows user, the ZIP format is the simplest and most compatible solution for everyone.

RELATED: How to extract files from a .tar file .gz or .tar.bz2 file on Linux

zip, unzip, and other utilities

You may already know that Linux and Unix-like operating systems like macOS have tools that allow you to create ZIP files and extract files from them, called zip and unzip . But there are a whole family of related utilities such as zipcloak zipdetails zipsplit and zipinfo .

We have Linux checked distributions to see if these utilities are included in the default installation. All utilities were available in Ubuntu 1

9.04, 18.10 and 18.04. They were also in Manjaro on 18.04. Fedora 29 contained zip and unzip but none of the other utilities, and that was also the case with CentOS.

Use the following command to install the missing items on Fedora 29:

  sudo dnf install perl IO Compress 

  zip install command for Fedora

Use the following command to install missing items on CentOS 7:

  sudo yum install perl-IO-Compress 

  Zip install command in Centos

If any of the Zip utilities in a Linux distribution is missing above was not mentioned, install the required package with the package management tool of this Linux distribution.

Creating a ZIP file with the zip command

To create a ZIP file, you must provide zip with the name of the archive file and the files it contains. You do not need to add the extension ".zip" to the archive name, but this does no harm.

To create a file named source_code.zip containing the entire C source files and header files in the current directory use the following command:

  zip source code * .c * .h 

  zip command in a terminal window

Each file is listed when added. The name of the file and the compression level reached for this file are displayed.

 Output of the command zip in a terminal window

If you look at the new ZIP archive, you can ensure that the file extension ".zip" is automatically zip was added.

  ls -l source_code.zip 

  ls on zip archive in a terminal window

If you use the output from zip when creating the ZIP file, do not use it The option -q (quiet).

  zip -q source code * .c * .h 

  Option -q quiet in a terminal window

Include directories in zip files

Use the option -r (recursive) and to include subdirectories in the ZIP file Enter the name of the subdirectory in the command line. Use this command to create a ZIP file as before and also include the archive subdirectory.

  zip -r -q source code archive / * .c * .h 

  -r recursive option in a terminal window

To take into account the person who created the files from the one you created Extracting a .zip file, it is often polite to create ZIP files containing the files in a directory. When the person receiving the ZIP file extracts them, all files are properly stored in a directory on their computer.

In the following command we archive the working directory and all subdirectories. Note that this command is issued from the parent directory of the folder work .

  zip -r -q source_code work / 

  Zipping the working directory in a terminal window

Setting the compression level

You can specify how much the files are compressed when added to the ZIP archive become. The range is 0 to 9, where 0 is no compression at all. The higher the compression, the longer it takes to create the ZIP file. For ZIP files of modest size, the time difference is not a significant penalty. For ZIP files of modest size, standard compression (level 6) is probably still sufficient.

To get zip for a given compression level, pass the number as an option on command line with a "-" as follows:

  zip -0 -r -q source code work / 

  -0 Compression option in a terminal window

The default compression level is 6. There is no need to provide the option -6 but this can not do any harm.

  zip -r -q source_code work / 

  Compression of level 6 in a terminal window

The maximum compression level is level 9.

  zip -9 -r -q source_code work / 

  -9 Compressing in a Terminal Window

With the selection of files and directories archived here, the difference between no Compression (Level 0) and Standard Compression (Level 6) is 400K. The difference between the standard compression and the highest compression level (level 9) is only 4 KB.

That does not seem like much, but for archives that contain hundreds or even thousands of files, the small amount of additional compression per file would be added to a worthwhile space savings.

Adding Passwords to ZIP Files

Adding passwords to ZIP files is easy. Use -e (encrypt), and you will be asked to enter your password and enter it again for confirmation.

  zip -e -r -q source_code work / 

  Add passwords to compress files in a terminal window

To decompress a ZIP file using the decompression command

Use to extract the files from a ZIP file, the decompression command and specify the name of the ZIP file. Note that you must specify the extension ".zip"

 . 

  Unzip command in a terminal window

Extracting the files is listed in the terminal window.

 Output of unzip in a terminal window

ZIP files do not contain any file ownership details. For all extracted files, the owner is set to the user who extracts them.

Just as with zip unzip has a -q (option quiet) so you will not have to see the file list when extracting the files.

  -q option source_code.zip 

  Extract -q option in a terminal window

Extract files to a target directory

To extract the files in a specific directory, use the option -d (directory) and specify the path to the directory where the archive is to be extracted.

  unzip -q source_code.zip -d ./development[19659069[unzipinindestinationinaterminalwindow" width="644" height="75" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

Extracting password-protected ZIP files

If a ZIP file was created with a password, unzip asks you for the password. If you do not specify the correct password, the files of unzip will not be extracted.

  unzip -q source_code.zip 

  The unzip command in a terminal window

If you do not want your password to be seen by other users or stored in your command history, you can do that Specify password in command line with option -P (password). (You must use a capital letter "P".)

  -P unpack fifty.treacle.cutlass -q source_code.zip 

  unpack with password option in a terminal window

Exclude files

If you do not want to extract a particular file or filegroup, use the option -x (exclude). In this example, we want to extract all files, except those files that end in the extension ".h".

  unzip -q sourcecode.zip -x * .h 

  unzip option in a terminal window ]

Overwrite Files

Let's say you extracted an archive, but accidentally extracted some of the extracted files deleted.

A quick fix would be to re-extract the files. However, if you try to extract the ZIP file to the same directory as before, unzip prompts you to make a decision to overwrite the files. One of the following answers is expected.

Apart from the answer r (rename), these responses are case-sensitive.

  • y: Yes, override this file
  • n: No, do not overwrite this file.
  • A: All, all files override.
  • N: None, do not overwrite files.
  • r: Rename, extract this file, but give it a new name. You will be asked to enter a new name.

  Override prompts in a terminal window

To force the unpack to overwrite existing files, use the -o (overwrite) option.

  unzip -o -q source_code.zip 

  Overwrite option in a terminal window

The most efficient way to replace the missing files would be to have only unzip Archive not located in the destination directory. Use the option -n (never overwrite).

  unpack -n sourcecode.zip 

  unzip with option -n never overwrite in a terminal window

search In a ZIP File

Often it is useful and instructive to keep a list of files in a ZIP file before extracting it. You can do this with the option -l (list archive). It is passed through less to make the issue manageable.

  unzip -l source_code.zip | less 

  Unpacking the list in a terminal window

The output shows the directories and files in the ZIP file, their length, and the time and date they were added to the archive. Press "q" to finish less .

 Listing archive output in a terminal window

There are other ways to take a look at a ZIP file containing various types of information as we shall see.

Adding a password with the zipcloak command

What can you do if you have created a ZIP file and forgot to add a password? You can quickly add a password to the ZIP file with the command zipcloak . Pass the name of the ZIP file to the command line. You will be prompted for a password. You must verify the password by typing it a second time.

  zipcloak source_code.zip 

  zipcloak in a terminal window

View file details With the command zipdetails

The zipdetails [Thecommand shows you a lot Information about the ZIP File. The only sensible way to deal with the amount of expenditure this order can deliver is to direct it through less .

  zip details source_code.zip | Less 

  Zip Details Command in a Terminal Window

Note that the information includes file names even if the ZIP file is password protected. This type of information is stored in the ZIP file as metadata and is not part of the encrypted data.

 Outputting Zip Details in a Terminal Window

Searching the File with the zipgrep Command

You can use the zipgrep command in in the files search a ZIP file. In the following example we would like to know which files in the ZIP file contain the text "keyval.h".

  zipgrep keyval.h source_code.zip 

  the command zipgrep in a terminal window

We can see that the files slang.c and getval.c contain the string "keyval.h". We can also see that there are two copies of each of these files in different directories in the ZIP file.

Viewing information with the zipinfo command

You can use the zipinfo command to review a ZIP file. As before, we pass the issue through less .

  zipinfo source_code.zip | less 

  Zipinfo command in a terminal window

From left to right, the output shows:

  • The file permissions
  • The version of the tool that created the zip file
  • The original size file
  • File descriptor (described below)
  • Compression method (deflation in this case)
  • Data and timestamp
  • File name and arbitrary directory

File descriptor consists of two characters. The first character is a "t" or a "b" to indicate a text or binary file. If it is a capital letter, the file is encrypted. The second character can be one of four characters. This character represents what type of metadata is contained in this file: none, an extended local header, an "extra field," or both.

  • -: If none exists, the character is a hyphen [19659086] l: if there is an extended local header but no additional field
  • x: if there is no extended local header but there is an additional field
  • X: if present An extended local header and an additional field

 zipinfo output in a terminal window

Share the file with the zipsplit command

If you do not want to send the zip file to another person, but there are size limitations or file transfer problems, you can use the command zipsplit to divide the original ZIP file into a set of smaller ZIP files.

The files -n [19459011MitderOption] (size) allow you to set a maximum size for each of the new ZIP files. This example splits file source_code.zip . We do not want any of the new ZIP files to be larger than 100K (102400 bytes).

  zipsplit -n 102400 source_code.zip 

  The zipsplit Command in a Terminal Window

The Size The size you choose must not be smaller than the size of any of the files in the ZIP file.

These commands allow you to create your own ZIP files, extract received ZIP files, and perform various other operations without ever leaving the Linux Terminal.




Source link