I used >>
logsave path/log.txt find . -name "*.zip" -exec sh -c 'unzip -l {} | head -n 7' \; to get the list of files inside zip files in a particular folder. Here is a problem if the filename contains one or more spaces it is just skipping the files. Dir structure is


..........File1.zip **without space in name

...........Fi le2.zip **With space in name

...........F i l e 3.zip **with spaces in name

For above case file names in file1.zip is listed only. Can anyone recommend a solution.


Try enclosing your zip filenames in quotes so that unzip knows it is only one file name:

find . -name "*.zip" -exec sh -c 'unzip -l "{}" | head -n 7' \; 

The reason that we need to quote the {} in the above command is that we are passing a string to sh to run as a command line.

I will try to explain what is happening step by step:

For each file that matches *.zip, we will run the command sh -c 'unzip -l "{}" | head -n 7. sh -c 'blahblablah' takes the argument after -c and runs it as if it was entered on the command line.

So given a filename File A.zip (with a space) we will execute the command line:

unzip -l File A.zip | head -n 7

Now, what happens when we don't quote the filename is that before calling unzip, the shell will split the argument list into separate arguments, resulting in a list:

"unzip" "-l" "File" "A.zip"

which means that unzip will try to open the zip file File and look for the item A.zip inside the archive.

If we enclose the argument in quotes the shell will expand to the following argument list:

"unzip" "-l" "File A.zip"

Win! unzip will now try to open the zip file "File A.zip" and list its contents.

  • thanks.. A simple but effective change. can you please explain how quote is used to ignore spaces. – shekhar Nov 7 '12 at 15:47
  • I've updated my answer to try and explain why the quotes are needed. – Joakim Nohlgård Nov 7 '12 at 16:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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