Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Dir.. **without space in name

...........Fi **With space in name

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

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

share|improve this question
up vote 2 down vote accepted

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 (with a space) we will execute the command line:

unzip -l File | 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" ""

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

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

"unzip" "-l" "File"

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

share|improve this answer
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .