5

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?

5

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.

3

On the contrary, unzip does too much. cp doesn't need to parse the filenames, all it needs to do is loop over them.

Unzip, on the other hand, needs to see if an argument is a wildcard, check the directory listing to see what matches the wildcard and then loop over those. And note that the shell already is capable of matching and expanding wildcards, so unzip is duplicating functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.