0

Summary
I need to "fix" a batch of .zip files, all over the filesystem, by:

  • replacing a " " (space) in their names with a "_", as well as in the names of their contents' files
  • re-zip them back

Stuck at unzipping because "$(dirname "$0")/$(basename "$0")" throws errors.

Longer desc.
File hierarchy:

banners/
       |
       germany/
       |      |
       |      active/
       |      |     |
       |      |     300x250/
       |      |     |      |
       |      |     |      file.jpg
       |      |     |      AC2 xx.zip
       |      |     |
       |      |     600x228/
       |      |            |
       |      |            file.jpg
       |      |            AC4 xx.zip
       |      nautics/
       |
       |
       austria/
       |
       ...

This is what I tried:

~/banners/> find . -name "*" -exec sh -c 'mv -- "$0" "$(0// /_)"' {} \;

Here I got rid of all the spaces in all the filenames/directory names from banners/ downwards.
AC2 xx.zip now looks like AC2_xx.zip

I now need to unzip these .zip files into a new subdir, at the same level as the .zip files themselves, named the same as these .zip files.

Here is the command I tried:

find . -name "*.zip" -exec sh -c 'unzip "$0" "$(dirname "$0")/$(basename "$0")"' {} \;

which gives me a bunch (full screen of) of errors like this one:

~>Archive: ./sun&sea/dugi_otok/970x250/SS4_970x250.zip
~>caution: filename not matched: ./sun&sea/dugi_otok/970x250/SS4_970x250.zip

For the life of me I cannot figure this one out.

Re-run remove spaces
If I get this to work and the files are unzipped, the plan is to re-run the mv command from the top level directory banners/ and again replace spaces with "_" again. This takes care of the .zip files' contents.

Re-zip
Finally, I need to re-zip these newly created subdirectories, named as the original .zip files and overwrite the existing .zip files (that were initially unzipped).

Basically re-zip this:

~> banners/germany/active/dugi_otok/300x250/AC2_xx/(newly unzipped contents)

into:

~> banners/germany/active/dugi_otok/300x250/AC2_xx.zip

I apologise for the confusion but I couldn't find a better way to explain this.

Alexander Starbuck
  • 1,139
  • 4
  • 18
  • 31

2 Answers2

0

basename does not strip the extension so your:

"$(dirname "$0")/$(basename "$0")"

just recreates $0. Replace with:

"$(dirname "$0")/$(basename -s ".zip" "$0")"

the -s option to basename strips a suffix

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

I now need to unzip these .zip files into a new subdir, at the same level as the .zip files themselves, named the same as these .zip files.

The unzip option -d is suitable for this:

find . -name "*.zip" -exec sh -c 'unzip "$0" -d "${0%.zip}"' {} \; 

With the command you tried the second argument of unzip was taken as the name of a file to extract from the zip file, and of course not found therein.

Armali
  • 18,255
  • 14
  • 57
  • 171