1. Introduction

As a system administrator, when dealing with compressed archives, encountering errors during extraction is not uncommon. One of the most common errors is the “filename not matched” message, which usually occurs when attempting to unzip an archive.

In this tutorial, we’ll explore the causes of this error and provide solutions to resolve it.

2. Understanding the Error

For example, consider a directory containing three zip files. The directory structure is shown below using the tree command, making it easier to understand the organization of files within the directory:

$ tree
.
├── baeldung_archive-1.zip
├── baeldung_archive-2.zip
└── baeldung_archive-3.zip

Next, we’ll extract all files from zip archives using the unzip command. Here, all zip files begin with the pattern “baeldung_archive-” and ends with “.zip“. Now, let’s use the wildcard character asterisk (*) for matching any sequence of characters between “baeldung_archive-” and “.zip“. Then, we’ll use the unzip command to extract the contents of the matching zip files:

$ unzip baeldung_archive-*.zip
Archive:  baeldung_archive-1.zip
caution: filename not matched:  baeldung_archive-2.zip
caution: filename not matched:  baeldung_archive-3.zip

Here, when we execute the command, we get the “filename not matched” error. *This problem arises when using wildcard patterns, like asterisks (*), in the unzip command without proper escaping*:

$ unzip -l baeldung_archive-*.zip
Archive:  baeldung_archive-1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
---------                     -------
        0                     0 files

Similarly, the unzip -l command also throws the same error. Usually, this command helps us list the contents of a zip archive without extracting them.

3. Different Methods of Pattern Handling

Generally, there are multiple ways of handling the patterns. Using single quotes and escaping the pattern using a backslash both serve to prevent the shell from expanding the wildcard character (*) before passing the argument to the unzip command.

3.1. Escape using Backslash

Now, let’s employ the backslash (\) as an escape character to interpret the asterisk (*) literally. Also, it ensures that it matches files with names starting with “baeldung_archive-” and ending with “.zip“. Further, it prevents the shell from expanding the asterisk before executing the command:

$ unzip -l baeldung_archive-\*.zip
Archive:  baeldung_archive-1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 10485760  05-07-2024 23:54   fileSet-1.txt
---------                     -------
 10485760                     1 file

Archive:  baeldung_archive-2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 15728640  05-07-2024 23:54   fileSet-2.txt
---------                     -------
 15728640                     1 file

Archive:  baeldung_archive-3.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 20971520  05-07-2024 23:55   fileSet-3.txt
---------                     -------
 20971520                     1 file

3 archives were successfully processed.

Next, let’s extract the files from the archives using the unzip command:

$ unzip baeldung_archive-\*.zip
Archive:  baeldung_archive-1.zip
  inflating: fileSet-1.txt

Archive:  baeldung_archive-2.zip
  inflating: fileSet-2.txt

Archive:  baeldung_archive-3.zip
  inflating: fileSet-3.txt

3 archives were successfully processed.

Finally, we’ve simultaneously extracted files from all three archives using the wildcard pattern. Let’s list all those files from the current path using the tree command:

$ tree
.
├── baeldung_archive-1.zip
├── baeldung_archive-2.zip
├── baeldung_archive-3.zip
├── fileSet-1.txt
├── fileSet-2.txt
└── fileSet-3.txt

The fileSet-1.txt, fileSet-2.txt, fileSet-3.txt from the respective baeldung_archive-1.zip, baeldung_archive-2.zip, baeldung_archive-3.zip archives.

3.2. Preserve String Using Quotes

When using single quotes (”), the entire string, including the wildcard pattern, is treated as a literal string by the shell. This means that no characters within the single quotes are subject to shell expansion, including wildcard expansion.

We commonly use single quotes, when we want to preserve the exact characters within the string:

$ unzip -l 'baeldung_archive-*.zip'
Archive:  baeldung_archive-1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 10485760  05-07-2024 23:54   fileSet-1.txt
---------                     -------
 10485760                     1 file

Archive:  baeldung_archive-2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 15728640  05-07-2024 23:54   fileSet-2.txt
---------                     -------
 15728640                     1 file

Archive:  baeldung_archive-3.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 20971520  05-07-2024 23:55   fileSet-3.txt
---------                     -------
 20971520                     1 file

3 archives were successfully processed.

Next, let’s extract the files from the archives using the unzip command:

$ unzip 'baeldung_archive-*.zip'
Archive:  baeldung_archive-1.zip
  inflating: fileSet-1.txt

Archive:  baeldung_archive-2.zip
  inflating: fileSet-2.txt

Archive:  baeldung_archive-3.zip
  inflating: fileSet-3.txt

3 archives were successfully processed.

Finally, we’ve simultaneously extracted files from all three archives using the wildcard pattern. Let’s list all those files from the current path using the tree command:

$ tree
.
├── baeldung_archive-1.zip
├── baeldung_archive-2.zip
├── baeldung_archive-3.zip
├── fileSet-1.txt
├── fileSet-2.txt
└── fileSet-3.txt

0 directories, 6 files

The fileSet-1.txt, fileSet-2.txt, fileSet-3.txt from the respective baeldung_archive-1.zip, baeldung_archive-2.zip, baeldung_archive-3.zip archives.

4. Conclusion

In summary, understanding the “filename not matched” error during archive extraction is crucial for efficient file management. We can achieve this by preserving the string using a single quote or escaping the asterisk using a backslash.

Encountering extraction errors is common when dealing with compressed archives. With attention to detail and an understanding of extraction processes, managing archives becomes a smoother and more efficient task.