How to use the command `unzip` (with examples)

How to use the command `unzip` (with examples)

The unzip command is used to extract files and directories from ZIP archives. It provides various options to define the path for extraction, list content, and extract specific files or directories. This article will illustrate each of these use cases with examples.

Use case 1: Extract all files/directories from specific archives into the current directory

Code:

unzip path/to/archive1.zip path/to/archive2.zip ...

Motivation: This use case is useful when you want to extract all files and directories from specific archives into the current directory.

Explanation: The command begins with unzip followed by the paths of the archive files you want to extract. You can specify multiple archives to extract at once.

Example output: If you run the command unzip archive.zip, all files and directories within the archive will be extracted into the current directory.

Use case 2: Extract files/directories from archives to a specific path

Code:

unzip path/to/archive1.zip path/to/archive2.zip ... -d path/to/output

Motivation: Extracting files and directories to a specific path can be useful when you want to organize the extracted content in a particular directory.

Explanation: In addition to the paths of the archive files, you need to include the -d option followed by the path where you want to extract the content.

Example output: Running the command unzip archive.zip -d /path/to/extraction will extract the files and directories from the archive to the specified path, /path/to/extraction.

Use case 3: Extract files/directories from archives to stdout

Code:

unzip -c path/to/archive1.zip path/to/archive2.zip ...

Motivation: Extracting files and directories to stdout can be useful when you want to preview the content of the archives without creating files on disk.

Explanation: The -c option tells unzip to extract the content to stdout instead of creating files on disk. You still need to provide the paths of the archive files to be extracted.

Example output: If you run the command unzip -c archive.zip, the content of the archive will be displayed in the console.

Use case 4: Extract the contents of the file(s) to stdout alongside the extracted file names

Code:

unzip -O gbk path/to/archive1.zip path/to/archive2.zip ...

Motivation: This use case is helpful when you want to extract the contents of the file(s) in a specific encoding format and view them alongside the extracted file names.

Explanation: The -O option is used to specify the encoding format, gbk in this example. This option ensures that the extracted content is displayed correctly when the encoding format is different from the default.

Example output: Running the command unzip -O gbk archive.zip will display the contents of the archive in the specified encoding format, gbk, along with the names of the extracted files.

Use case 5: List the contents of a specific archive without extracting them

Code:

unzip -l path/to/archive.zip

Motivation: Listing the contents of an archive without extracting them can be useful when you want to get an overview of what’s inside the archive without creating any files on disk.

Explanation: The -l option is used to list the contents of the archive without extracting them. You only need to provide the path of the archive file.

Example output: When you run the command unzip -l archive.zip, it will display a list of the files and directories contained in the archive.

Use case 6: Extract a specific file from an archive

Code:

unzip -j path/to/archive.zip path/to/file_in_archive1 path/to/file_in_archive2 ...

Motivation: Extracting a specific file from an archive can be helpful when you only need certain files and don’t want to extract the entire archive.

Explanation: The -j option tells unzip to extract the specified file(s) without creating any directories. You need to provide the path to the archive file, followed by the path(s) of the file(s) you want to extract.

Example output: Running the command unzip -j archive.zip file.txt will extract the file file.txt from the archive, without creating any directories.

Conclusion:

The unzip command is a versatile tool for extracting files and directories from ZIP archives. Whether you want to extract all content, specify output paths, extract to stdout, list contents or extract specific files, unzip provides a range of options to suit your needs.

Related Posts

Using Git Blame to Track Changes in a Git Repository (with examples)

Using Git Blame to Track Changes in a Git Repository (with examples)

Git Blame is a useful command-line tool that allows you to track changes made to a file in a Git repository.

Read More
How to use the command 'createrepo' (with examples)

How to use the command 'createrepo' (with examples)

The ‘createrepo’ command is used to initialize an RPM repository in the given directory, including all XML and SQLite files.

Read More
Git brv (with examples)

Git brv (with examples)

Git is a widely-used version control system that allows developers to track changes to their codebase.

Read More