Linux で Zip アーカイブの内容を一覧表示する方法

LinuxLinuxBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

Introduction

This tutorial will guide you through the process of listing the contents of a zip archive on your Linux system. Whether you are a developer, system administrator, or just a curious Linux user, understanding how to work with zip files from the command line is a valuable skill. We will cover the basics of zip archives, demonstrate the steps to list their contents, and provide practical use cases and examples to help you manage your files more efficiently.

Installing ZIP Utilities

To work with zip archives in Linux, we need to have the appropriate utilities installed. The primary tools we need are the zip and unzip commands.

Let us check if these utilities are already installed on your system. Open a terminal and run the following commands:

which zip
which unzip

If these commands return paths like /usr/bin/zip and /usr/bin/unzip, then the tools are already installed. If you see no output, we need to install them.

On Ubuntu, we can install the zip utilities using the apt package manager:

sudo apt update
sudo apt install -y zip unzip

Let us verify the installation:

zip --version
unzip -v | head -n 1

The output should show the installed versions of the zip utilities, similar to this:

Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

Now that we have the necessary tools installed, we are ready to create and work with zip archives.

Creating a Sample Zip Archive

Before we learn how to list the contents of a zip archive, let us create a sample zip file to work with. This will give us a practical example to use throughout this tutorial.

First, let us create a directory to store our sample files. Run the following commands in your terminal:

mkdir -p ~/project/zip_demo
cd ~/project/zip_demo

Now, let us create a few sample files with different content:

echo "This is a text file for our zip demo." > sample_text.txt
echo "Another text file with different content." > another_text.txt
mkdir -p documents
echo "This file is inside a subdirectory." > documents/nested_file.txt

Let us verify that our files were created correctly:

ls -la
ls -la documents

You should see output similar to:

total 20
drwxrwxr-x 3 labex labex 4096 Jul 15 10:00 .
drwxr-xr-x 3 labex labex 4096 Jul 15 10:00 ..
-rw-rw-r-- 1 labex labex   39 Jul 15 10:00 another_text.txt
drwxrwxr-x 2 labex labex 4096 Jul 15 10:00 documents
-rw-rw-r-- 1 labex labex   35 Jul 15 10:00 sample_text.txt

total 12
drwxrwxr-x 2 labex labex 4096 Jul 15 10:00 .
drwxrwxr-x 3 labex labex 4096 Jul 15 10:00 ..
-rw-rw-r-- 1 labex labex   36 Jul 15 10:00 nested_file.txt

Now let us create a zip archive containing these files:

zip -r sample_archive.zip sample_text.txt another_text.txt documents/

The -r option tells the zip command to recursively include all files and subdirectories.

You should see output similar to:

  adding: sample_text.txt (stored 35 bytes)
  adding: another_text.txt (stored 39 bytes)
  adding: documents/ (stored 0 bytes)
  adding: documents/nested_file.txt (stored 36 bytes)

Now we have created a zip archive named sample_archive.zip that contains our sample files and directories. In the next step, we will learn how to list the contents of this archive.

Basic Methods to List Zip Contents

Now that we have created a sample zip archive, let us learn how to list its contents. The primary command for this purpose is unzip with various options.

Using the -l Option (List)

The most common way to list the contents of a zip archive is to use the unzip command with the -l option. This option displays a table showing basic information about each file in the archive.

Let us navigate to our zip demo directory and list the contents of our sample archive:

cd ~/project/zip_demo
unzip -l sample_archive.zip

You should see output similar to:

Archive:  sample_archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       35  2023-07-15 10:00   sample_text.txt
       39  2023-07-15 10:00   another_text.txt
        0  2023-07-15 10:00   documents/
       36  2023-07-15 10:00   documents/nested_file.txt
---------                     -------
      110                     4 files

The output shows:

  • The size of each file in bytes
  • The date and time when each file was last modified
  • The name of each file or directory
  • A summary showing the total size and number of files

Using the -Z Option (ZipInfo)

Another option is to use the -Z option, which provides a more concise listing:

unzip -Z sample_archive.zip

You should see output similar to:

Archive:  sample_archive.zip
Zip file size: 676 bytes, number of entries: 4
-rw-rw-r--  3.0 unx       35 tx defN 23-Jul-15 10:00 sample_text.txt
-rw-rw-r--  3.0 unx       39 tx defN 23-Jul-15 10:00 another_text.txt
drwxrwxr-x  3.0 unx        0 bx stor 23-Jul-15 10:00 documents/
-rw-rw-r--  3.0 unx       36 tx defN 23-Jul-15 10:00 documents/nested_file.txt
4 files, 110 bytes uncompressed, 110 bytes compressed:  0.0%

This format displays file permissions, compression information, and other details in a more compact form.

In the next step, we will explore more advanced options for listing zip contents and understand how to interpret the results.

Advanced Options for Viewing Zip Contents

Now let us explore some advanced options for listing zip archive contents, which provide more detailed information about the files within the archive.

Using the -v Option (Verbose)

The -v option provides more detailed information about each file in the archive, including the compression method, compression ratio, and CRC (Cyclic Redundancy Check) values:

cd ~/project/zip_demo
unzip -v sample_archive.zip

You should see output similar to:

Archive:  sample_archive.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
      35  Stored       35   0% 2023-07-15 10:00 a1b2c3d4  sample_text.txt
      39  Stored       39   0% 2023-07-15 10:00 e5f6g7h8  another_text.txt
       0  Stored        0   0% 2023-07-15 10:00 00000000  documents/
      36  Stored       36   0% 2023-07-15 10:00 i9j0k1l2  documents/nested_file.txt
--------          -------  ---                            -------
     110             110   0%                            4 files

This output provides the following additional information:

  • Method: The compression method used (in this case, "Stored" means no compression)
  • Size: The compressed size of the file
  • Cmpr: The compression ratio as a percentage
  • CRC-32: A checksum value used to verify file integrity

Filtering Output with Grep

Sometimes you may want to check if a specific file exists in a large zip archive. You can combine the unzip -l command with grep to search for specific files:

unzip -l sample_archive.zip | grep "nested_file"

This command will only show entries that contain the text "nested_file" in their names:

       36  2023-07-15 10:00   documents/nested_file.txt

Count Files in a Zip Archive

To count the number of files in a zip archive, you can use the following command:

unzip -l sample_archive.zip | grep -v "Archive:" | grep -v "Length" | grep -v "-------" | grep -v "^$" | wc -l

This command filters out header and footer lines and counts the remaining lines, which correspond to files and directories in the archive.

The output should be:

4

Now you know how to use advanced options to get more detailed information about the contents of zip archives, search for specific files, and count the files in an archive.

Practical Use Cases and Examples

Now that we have learned how to list the contents of zip archives, let us explore some practical scenarios where these skills can be useful. We will work with more realistic examples that you might encounter in daily Linux usage.

Creating a Backup Archive

Creating backups is a common use case for zip archives. Let us create a backup of our configuration files:

cd ~/project/zip_demo
mkdir -p config
echo "server_name=example.com" > config/server.conf
echo "user=admin" > config/user.conf
echo "port=8080" > config/port.conf

## Create a dated backup archive
zip -r backup_$(date +%Y%m%d).zip config/

The command creates a backup file with today's date in the format backup_YYYYMMDD.zip.

Let us verify the contents of our backup:

unzip -l backup_*.zip

You should see output listing all the configuration files we just backed up.

Comparing Archive Contents

Sometimes you need to compare the contents of two zip archives to see what has changed. Let us create a second backup with some changes:

## Modify a file
echo "port=9090" > config/port.conf
## Add a new file
echo "debug=true" > config/debug.conf
## Create a new backup
zip -r backup_new.zip config/

Now let us compare the contents of the two backup files:

## List files in the first backup
echo "Original backup:"
unzip -l backup_*.zip | grep -v "Archive:" | grep -v "Length" | grep -v "-------"

## List files in the new backup
echo -e "\nNew backup:"
unzip -l backup_new.zip | grep -v "Archive:" | grep -v "Length" | grep -v "-------"

You will notice that the new backup includes the additional debug.conf file, and the port.conf file might have a different size due to our modifications.

Extracting Specific Files

You can use the information obtained from listing zip contents to extract only specific files:

## Create a directory for extraction
mkdir -p extracted

## List the contents first
unzip -l backup_new.zip

## Extract only the debug.conf file
unzip backup_new.zip "config/debug.conf" -d extracted/

Let us verify that only the requested file was extracted:

ls -la extracted/config/

You should see only the debug.conf file in the extracted directory.

By mastering these practical examples, you now have the skills to effectively manage zip archives in various real-world scenarios. From creating backups to comparing archive contents and selectively extracting files, these techniques will help you manage your files more efficiently in Linux.

Summary

In this tutorial, you have learned how to work with zip archives in Linux, focusing on listing their contents. Here is a recap of what we covered:

  1. Installing the required zip utilities on your Linux system
  2. Creating sample zip archives to work with
  3. Using basic commands like unzip -l to list archive contents
  4. Exploring advanced options such as unzip -v for detailed information
  5. Applying these skills in practical scenarios like creating backups, comparing archives, and extracting specific files

These skills are valuable for file management, troubleshooting, backups, and various other tasks in Linux. You can now confidently inspect zip archives without extracting them, which can save time and disk space when working with large archives.

Some key commands to remember:

  • unzip -l archive.zip: Basic listing of archive contents
  • unzip -v archive.zip: Verbose listing with compression details
  • unzip -Z archive.zip: Concise listing format
  • unzip -l archive.zip | grep "filename": Searching for specific files

With these commands at your disposal, you can efficiently manage zip archives in your Linux environment.