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.