2

I have two related questions in a Linux environment.

I am confused when to use backward-slash \ and combining with asterisk "*". When I try to run Unzip *.zip the command fails until I escape it. Why is it so?

developer@tz:~/f$ bash -version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
developer@tz:~/f$ unzip *.zip
Archive:  abstract.zip
caution: filename not matched:  date-function.zip
developer@tz:~/f$ unzip \*.zip
Archive:  abstract.zip
  inflating: abstract.xsl            

Archive:  date-function.zip
warning [date-function.zip]:  zipfile is empty

1 archive was successfully processed.
1 archive had warnings but no fatal errors.
developer@tz:~/f$ ls *.zip
abstract.zip  date-function.zip
developer@tz:~/f$ ls \*.zip
ls: cannot access '*.zip': No such file or directory

On the similar issue regarding one-liner functions.

Suppose I have zip files: foo.zip and bar.zip in same directory and I need to extract both; when I do:

ls *.zip | xargs -IZIP  basename -s .zip ZIP

the above commands will list: foo and bar on konsole, but when I run:

ls *.zip | xargs -IZIP unzip ZIP -d $( basename -s .zip ZIP )

it fails to run because when I pass the results of base, it does NOT get a value, hence it fails.

  1. When do I use "\" or "*" or combine both when invoking konsole commands?
  2. How can I extract the zip file to a directory with its own name?
  • 1
    Notes: 1) It isn't a good idea to parse the output of ls. 2) On my system, unzip(1) contains a great deal of information under the "Examples" heading. Perhaps the same is true of yours? – Fox Jul 3 '17 at 9:08
4

The usage of unzip in particular may lead to some confusion. It can be called as

$ unzip x.zip file-1 file-2 file-3

where file-1 and the like are files contained within x.zip. For the remainder of this answer, I will be assuming the current directory contains two .zip files: x.zip and y.zip.

When the shell performs a glob expansion (wildcards such as * or ?), each matching file becomes a separate argument. So

$ unzip *.zip

turns into

$ unzip x.zip y.zip

Remember, this syntax means "extract the file y.zip from the archive x.zip". In order to allow multiple archives, unzip will also process wildcards. If you want to let unzip handle the wildcards instead of the shell, you have to either quote or escape them:

$ unzip \*.zip

or

$ unzip '*.zip'
  • Thanks for your comments.. You brought some light and helped me dig better..!! – Noel Alex Makumuli Jul 7 '17 at 3:45

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.