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.
2 Answers
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 7Now, 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.
You can use it with for too.
for file in *.zip; do unzip -l "$file" | head -n 7; done