Unzip source arguments

argumentscommand linewildcardszip

To copy a bunch of zip files from one location to another:

$ cp *.zip destination/

To unzip a bunch of zip files to a directory:

$ unzip \*.zip -d destination/

man unzip says:

The backslash before the asterisk is only required if the shell
expands wildcards, as in Unix

What confuses me is that the two commands do not seem to behave the same way. Why wouldn't they? Is there a way to know or guess how each command behaves, short of just remembering or reading the man pages every time?

Is it different because cp is implemented to take several source arguments while unzip isn't? unzip can take a string with wildcards, why not implement taking in several source files?

What is the standard way to take source arguments? Is it cp that actually does too much?

Best Answer

If you have two zip files a.zip and b.zip in your current directory, then

$ cp *.zip destination/

expands to

$ cp a.zip b.zip destination/

The semantics for cp is to copy both a.zip and b.zip to destination.

If you type

$ cp \*.zip destination/

it simply "expands" to

$ cp '*.zip' destination/

i.e. it will try to copy a single file named "*.zip" to destination, which is not what you want.

On the other hand if you type

$ unzip *.zip -d destination/

it will again expand to

$ unzip a.zip b.zip -d destination/

The semantics for unzip is to find a file named "b.zip" inside the archive "a.zip", which is again not what you want.

If you type

$ unzip \*.zip -d destination/

the unzip command does not simply try to unzip the file called *.zip but it will unzip all files ending with .zip.

The difference is that both commands interpret their arguments differently.

Related Question