Unzip on linux examples

Practical Examples of the Unzip Command in Linux

Got a zip file in the terminal? Learn how to use the unzip command in Linux with these practical examples.

If you have a zip compressed file, you can unzip it in the Linux command line. The unzip command in Linux is quite versatile and you can use it do a lot more than just extracting zip file.

I have discussed how to gzip a folder in Linux in the past. It’s time to see various usage of the unzip command. Before you do that, make sure that unzip has been installed on your system. You can use your distribution’s package manager to install the command.

On Ubuntu and Debian, you can use this command:

Once you have verified that, let’s see how to use unzip in Linux terminal.

Unzip command in Linux

The unzip command has a really simple syntax:

If you use it to extract a zip file without any option, it will extract all the files in the current directory:

unzip webdesign.zip Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PAJF07-322.eps inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTL-394.eps inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt

And that’s not what you would want most of the time. It’ll just flood your current directory with all the extracted files.

ls 339252-PAJF05-394.ai 339252-PALBTI-224.ai 339252-PALBTM-53.jpg 'License premium.txt' 339252-PAJF07-322.eps 339252-PALBTL-394.eps 'License free.txt' webdesign.zip

1. Unzip to a directory

The expected behavior is that you should have the files extracted to a certain directory, normally with the same name as the zip file.

You can specify the target directory where you want to extract the files.

unzip -d target_directory zip_file

If the target directory doesn’t exist, it will be created. You cannot create nested directories in this manner though.

Do note that you can also put the target directory at the end but not all options can be added at the end.

unzip zip_file -d target_directory

2. See the content of the zip file without extracting

If you want to see what the zip file contains, you son’t always have to extract it first. You can use the -l option and it will show the content of the zip file.

Читайте также:  File system and directories in linux

As you can see, it also shows the timestamp of the files and the actual size of the individual files in bytes.

[email protected]:$ unzip -l webdesign.zip Archive: webdesign.zip Length Date Time Name --------- ---------- ----- ---- 205358 2018-06-18 23:14 339252-PAJF05-394.ai 996906 2018-06-18 23:14 339252-PAJF07-322.eps 213968 2018-06-20 00:00 339252-PALBTI-224.ai 1005362 2018-06-20 00:00 339252-PALBTL-394.eps 305531 2018-06-20 00:00 339252-PALBTM-53.jpg 1462 2018-06-20 09:45 License free.txt 1116 2018-06-20 09:45 License premium.txt --------- ------- 2729703 7 files

If you want, you can get more information like the compressed size, compression ratio by using the verbose mode with -v option. The CRC-32 in the output is the cyclic redundancy check.

[email protected]:$ unzip -v webdesign.zip Archive: webdesign.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 205358 Defl:N 142097 31% 2018-06-18 23:14 792f2380 339252-PAJF05-394.ai 996906 Defl:N 221228 78% 2018-06-18 23:14 440390d3 339252-PAJF07-322.eps 213968 Defl:N 147568 31% 2018-06-20 00:00 cdb64dfc 339252-PALBTI-224.ai 1005362 Defl:N 226727 77% 2018-06-20 00:00 aed3d97a 339252-PALBTL-394.eps 305531 Defl:N 183363 40% 2018-06-20 00:00 e4ced90f 339252-PALBTM-53.jpg 1462 Defl:N 728 50% 2018-06-20 09:45 0eb9e17b License free.txt 1116 Defl:N 558 50% 2018-06-20 09:45 165dd84e License premium.txt -------- ------- --- ------- 2729703 922269 66% 7 files

3. Overwrite all the files without prompting

If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o .

unzip -o -d target_directory zip_file

4. Do not overwrite any files -n

If you don’t want any existing file to be overwritten by newly extracted files, use the -n option (stands for never overwrite).

unzip -n -d target_directory zip_file

5. Update files and create if necessary

This is slightly different the overwriting all the files. In this case, only those files will will be overwritten that have newer timestamp than the existing files. If a file doesn’t exist, it will be created.

You can achieve that with option -u:

unzip -u -d target_directory zip_file

6. Freshen existing files but create none

Slight change from the previous example here. In this one, it will update the existing files if they have older timestamp but it won’t create any new files even if they don’ exist.

The option -f allows you to do that:

unzip -f -d target_directory zip_file

7. Extract in quiet mode

When you unzip a file, it shows all the files that have been extracted on the display. Now imagine a zip file that has hundreds of files in it. If you extract it, your screen will be cluttered with the output.

Читайте также:  What is unix and linux operating system

You can use the quiet mode with option -q and you won’t see anything on the display:

unzip -q -d target_directory zip_file

8. Exclude files from extraction

You can also exclude certain files or certain type of files from being extracted.

unzip zip_file -x file_to_exclude

In my example, let’s say I don’t want to extract any .eps files.

[email protected]:$ unzip webdesign.zip -x *.eps Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt

Those were some of the most common examples of the unzip command in Linux. You can always check its man page to learn about more options.

Do you use some other option with unzip frequently? Why not share it with us in the comments?

Источник

Unzip a Directory in Linux: 10 Example Commands

LinuxCapable

Linux is a powerful and versatile operating system that is widely used by developers, system administrators, and tech enthusiasts. Unzipping directories is one of the many tasks you may find yourself performing in Linux. In this article, we’ll explore the concept of zip files, why it’s important to unzip files in Linux, and how to unzip a directory using 10 example commands.

What is a Zip File?

A zip file is a compressed file format that stores multiple files and directories into a single archive. It’s widely used to save storage space, speed up file transfers, and simplify file management. When you unzip a file, you extract its contents, making them accessible for use.

Why Unzip Files in Linux?

Unzipping files in Linux is essential for various reasons:

  1. To access and use the contents of a zip file.
  2. To reduce storage space on your system.
  3. To speed up file transfers and downloads.
  4. To simplify file management and organization.

How to Unzip a Directory in Linux

Before diving into the example commands, let’s look at some prerequisites and basics of the unzip command.

Prerequisites

To unzip files in Linux, you’ll need the unzip utility. Most Linux distributions come with unzip preinstalled. If it’s not installed on your system, you can install it using your package manager:

sudo apt install unzip # Debian/Ubuntu sudo yum install unzip # CentOS/RHEL sudo pacman -S unzip # Arch Linux apk add unzip #Alpine Linux sudo zypper install unzip #openSUSE emerge unzip #Gentoo

The Unzip Command

Basic Unzip Syntax

The basic syntax for the unzip command is:

unzip [options] [archive.zip] [-d destination]

Where [options] are optional flags, [archive.zip] is the zip file you want to unzip, and [-d destination] specifies the directory where you want to extract the files.

Unzip Options

Some common unzip options are:

  • -o : Overwrite existing files without prompting.
  • -n : Do not overwrite existing files.
  • -d : Specify the destination directory.
  • -j : Junk the path, only extracting the file names.
  • -q : Perform operations quietly.
  • -t : Test the integrity of the zip file.
Читайте также:  Консольный клиент telegram linux

10 Example Unzip Commands in Linux

Now that we have a basic understanding of the unzip command and its options let’s dive into 10 example commands to unzip directories in Linux.

1. Unzip a Single File

To unzip a single file, you can use the following command:

This command extracts the contents of archive.zip into the current working directory.

2. Unzip to a Different Directory

If you want to extract the contents of a zip file to a specific directory, use the -d option followed by the destination directory:

unzip archive.zip -d /path/to/destination

This command will unzip the contents of archive.zip into the /path/to/destination directory.

3. Unzip Multiple Files

To unzip multiple files at once, use the following command:

This command will unzip all zip files in the current working directory.

4. Unzip and Overwrite Existing Files

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

This command will unzip archive.zip and overwrite any existing files with the same names in the destination directory.

5. Unzip Without Overwriting Existing Files

To unzip a file without overwriting existing files, use the -n option:

This command will unzip archive.zip without overwriting any existing files with the same names in the destination directory.

6. Unzip and Preserve Directory Structure

If you want to unzip a file and preserve its directory structure, use the default unzip command without any additional options:

This command will unzip archive.zip and maintain the original directory structure of the compressed files.

7. Unzip and Show Progress

To unzip a file and show the progress of the extraction process, use the -v option:

This command will display the progress of unzipping archive.zip , providing detailed information about each file being extracted.

8. Unzip and Test Archive Integrity

To test the integrity of a zip file before extracting it, use the -t option:

This command will check archive.zip for errors and report any issues found.

9. Unzip and Set File Permissions

To unzip a file and set specific file permissions, use the -X option followed by the desired permissions:

This command will unzip archive.zip and apply the original file permissions as stored in the zip file.

10. Unzip and Exclude Files

If you want to unzip a file but exclude specific files or patterns, use the -x option followed by the pattern to exclude:

unzip archive.zip -x 'exclude_pattern*'

This command will unzip archive.zip , excluding any files that match the ‘exclude_pattern*’ pattern.

Conclusion

Unzipping directories in Linux is a common task that is made easy by the powerful unzip command. With various options and example commands provided in this article, you can now confidently unzip files in Linux and manage your compressed files with ease.

Источник

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