Open zip files with linux

How to Unzip Files in Linux

ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories.

In this tutorial, we will explain how to unzip files in Linux systems through the command line using the unzip command.

Installing unzip #

unzip is not installed by default in most Linux distributions, but you can easily install it using the package manager of your distribution.

Install unzip on Ubuntu and Debian #

Install unzip on CentOS and Fedora #

How to Unzip a ZIP file #

In it’s simplest form, when used without any option, the unzip command extracts all files from the specified ZIP archive to the current directory.

As an example, let’s say you downloaded the WordPress installation ZIP file. To unzip this file to the current directory, you’d simply run the following command:

ZIP files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command.

You must have write permissions on the directory where you are extracting the ZIP archive.

Suppress the Output of the unzip Command #

By default, unzip prints the names of all the files it’s extracting and a summary when the extraction is completed.

Use the -q switch to suppress the printing of these messages.

Unzip a ZIP File to a Different Directory #

To unzip a ZIP file to a different directory than the current one, use the -d switch:

unzip filename.zip -d /path/to/directory

For example, to unzip the WordPress archive latest.zip to the /var/www/ directory, you’d use the following command:

sudo unzip latest.zip -d /var/www

In the command above, we are using sudo because usually the user we are logged in as doesn’t have write permissions to the /var/www directory. When ZIP files are decompressed using sudo , the extracted files and directories are owned by the user root.

Unzip a Password Protected ZIP file #

To unzip a file that is password-protected, invoke the unzip command with the -P option followed by the password:

unzip -P PasswOrd filename.zip

Typing a password on the command line is insecure and should be avoided. A more secure option is to extract the file normally without providing the password. If the ZIP file is encrypted, unzip will prompt you to enter the password:

archive: filename.zip [filename.zip] file.txt password: 

unzip will use the same password for all encripted files as long as it is correct.

Читайте также:  Linux interface config file

Exclude Files when Unzipping a ZIP File #

To exclude specific files or directories from being extracted, use the -x option followed by space-separated list of archive files you want to exclude from extracting:

unzip filename.zip -x file1-to-exclude file2-to-exclude

In the following example we are extracting all files and directories from the ZIP archive except the .git directory:

Overwrite Existing Files #

Let’s say you’ve already unzipped a ZIP file and you are running the same command again:

By default, unzip will ask you whether you like to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all files, or rename the current file.

Archive: latest.zip replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename: 

If you want to overwrite existing files without prompting, use the -o option:

Use this option with caution. If you made any changes to the files, the changes are lost.

Unzip a ZIP File Without Overwriting Existing Files #

Let’s say you’ve already unzipped a ZIP file, and you made changes to some files, but you accidentally deleted few files. You want to keep the changes and to restore the deleted files from the ZIP archive.

In this case, use the -n option which forces unzip to skip the extraction of a file that already exists:

Unzip Multiple ZIP Files #

You can use regular expressions to match multiple archives.

For example, if you have multiple ZIP files in your current working directory you can unzip all files using only one command:

Note the single quotes around the *.zip . If you forgot to quote the argument, the shell will expand the wildcard character, and you will get an error.

List the Contents of a Zip File #

To list the contents of a ZIP file, use the -l option:

In the example below, we are listing all WordPress installation files:

The output will look like this:

Archive: latest.zip Length Date Time Name --------- ---------- ----- ---- 0 2018-08-02 22:39 wordpress/ 3065 2016-08-31 18:31 wordpress/xmlrpc.php 364 2015-12-19 12:20 wordpress/wp-blog-header.php 7415 2018-03-18 17:13 wordpress/readme.html . . 21323 2018-03-09 01:15 wordpress/wp-admin/themes.php 8353 2017-09-10 18:20 wordpress/wp-admin/options-reading.php 4620 2017-10-24 00:12 wordpress/wp-trackback.php 1889 2018-05-03 00:11 wordpress/wp-comments-post.php --------- ------- 27271400 1648 files 

Conclusion #

unzip is a utility that helps you list, test, and extract compressed ZIP archives.

To create a ZIP archive on a Linux system, you’ll need to use the zip command .

Feel free to leave a comment if you have any questions.

Источник

How To Unzip A File In Linux | Unzip Syntax | 8 Unzip Commands

How To Unzip A File In Linux | Unzip Syntax | 8 Unzip Commands

If you have a compressed zip file, you can unzip it using the Linux command line. Linux’s unzip command is highly flexible just like zip commands and can be used for much more than simply extracting zip files. Files from a ZIP archive, which are often seen on MS-DOS computers, can be listed, tested, or extracted using Unzip. If no settings are provided, the default behavior i.e. with no options will extract all files from the supplied ZIP archive into the current file and all subdirectories beneath it.

Читайте также:  Linux курсы с практикой

Unzip syntax

First of all, you need to install the unzip package. To do so, type in a terminal command:

sudo apt-get install unzip

Once all the packets have been installed, to unzip the archive files type in a terminal command:

A more useful tool is 7z, which zips and unzips a range of compression formats, notably lzma, usually the protocol offering the highest compression rates.

sudo apt-get install p7zip-full

Extract the zip file with Linux

  1. Open the Files app and navigate to the directory where the zip file is located.
  2. Locate the file which you want to unzip.
  3. Right-click on the file and the context menu will appear with the list of options.
  4. Select the “Extract Here” option to unzip files into the present working directory or choose “Extract to. ” for a different directory.

Unzip arguments in Linux

Some of the most commonly used arguments by unzip are listed below:

It tells the path of the zip archive(s). When a wildcard is used in the file specification, the operating system chooses the order in which each matching file is processed (or file system). The path itself cannot be a wildcard; only the filename can.

A list of archive members to process that can be optional, delimited by spaces. (VMS versions that have VMSCLI specified during compilation had to delimit files using commas. (For further information, see -v in the section below that lists your options.) You can match several members using regular expressions (wildcards).

An optional list of archive members to be excluded from processing. This option can be used to exclude any files that are in subdirectories because wildcard characters often match (‘/’) directory separators (for exceptions see the option -W).

If specified, files will be extracted to the target directory exdir. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the current working directory). This option need not appear at the end of the command line; it is also accepted before the zip file specification (with the normal options), immediately after the zip file path, or between the file(s) and the -x option.

Unzip commands/Options in Linux

To unzip a file, you do not need to add any options like the zip command. With unzip, the default behavior is to always seek permission before overwriting current files. Using unzip, you can extract every letter from the archive. Create any necessary subdirectories, then unzip into the current directory and any subdirectories below it options and commands that unzip accepts and uses are:

1. Unzip a file to a different directory

To unzip a ZIP file to a different directory than the current folder, we use the -D (Directory) Option. Go to the file manager and click on the zip file you want to unzip. Now open the terminal window and run the following command:

Command: $ unzip filename.zip -d /path/to/directory

2. Unzip tar/tar.gz/tgz files to a specific directory

Читайте также:  Alt linux основные команды терминала

Most files in Linux are compressed using the tar format. A .tar file is a collection of uncompressed files, sometimes known as a tarball. Since .tar doesn’t compress anything, it requires a separate compression utility for compression. The tar command allows you to create tar archive files as well as decompress them. This technique can be used on other file types/ file formats also. Using tar UtilityA tar.gz file is a combination of a .tar file and a .gz file. By default, the tar command will extract files to your current directory. Go to the terminal window and run the following command:

Command: $ tar -xf file-name.tar -C /path/to/directory

The -x option tells tar to extract the files. You can also use xargs with tar to create a tar.gz archive and populate it with files from the find command. Note: Some graphical interfaces include a tool for managing tar.gz files without the command line. Working with the graphical interface is a little difficult task.

3. Unzip a password-protected zip file

To unzip a file that is password-protected, invoke the unzip command with the -P option i.e -P (Password) Option followed by the password:

Command: $ unzip -P PasswOrd filename.zip

4. Exclude Files when Unzipping a ZIP File

To exclude specific files or directories from being extracted, use the -X (Exclude) option in the terminal :

Command: $ unzip filename.zip -x file1-to-exclude file2-to-exclude

5. Suppress the Output of the unzip Command

By default, unzip prints the names of all the files it’s extracting and a summary when the extraction is completed. Use the -q switch to suppress the printing of these messages.

Command: $ unzip -q filename.zip

6. Overwrite existing files

If you want to overwrite existing files without prompting, it can be done using the -o option. To run the -o (overwrite) option open the terminal window and run the following command:

Command: $ unzip -o filename.zip

7. Unzip a zip file without overwriting existing files

This command can be performed using the -n option which forces unzip to skip the extraction of a file that already exists:

Command: $ unzip -n filename.zip

8. List the Contents of a zip file

To list the contents of a given zip file we use the -l (list archive) option. To execute the -l option to the current file, open the windows terminal and type the following command:

Command: $ unzip -l filename.zip

Summary

The unzip utility is an extremely effective and straightforward tool for transferring compressed files over a network or even between operating systems. From the above tutorial, you can easily unzip files in Linux systems through the command line using the unzip command.

Suggested reads:

Shivangi Vatsal

I am a storyteller by nature. At Unstop, I tell stories ripe with promise and inspiration, and in life, I voice out the stories of our four-legged furry friends. Providing a prospect of a good life filled with equal opportunities to students and our pawsome buddies helps me sleep better at night. And for those rainy evenings, I turn to my colors.

Источник

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