Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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..

..........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.

share|improve this question
add comment

1 Answer

up vote 1 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 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.

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 Gebart Nov 7 '12 at 16:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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