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 have a bunch of .zip and .rar files. Many of these files contain subfolders in the archive. I want to remove all the folders from the zip files but keep the other content.

e.g.

original.zip
- /foo/bar1.jpg 
- /foo/bar2.jpg
- /foo/bar3.jpg
- /foo/bar4.jpg

>>>

new.zip
- bar1.jpg 
- bar2.jpg
- bar3.jpg
- bar4.jpg

This can be a pure terminal command or a bash script. I need to be able to run it on 100s of zip/rar files at once.

update: This doesn't seem to be possible w/o recreating the zips + rars.

So I have written a little bash script, it creates two dirs, "cleaned" for post-processed files, and "temp" for the unzipped files. It loops through all archives in the current directory (.cbr and .cbz files), it creates a "clean name" for each new file, unzips the old file to the temp dir, creates a new archive from the temp files and saves it with the clean name in the "cleaned" directory, then clears out the temp folder. after all files are processes the "temp" dir is recursively removed. here's what it looks like:

#!/bin/bash
echo "comic cleaning!";
echo "---------------";
dir=$(pwd)/;
tmp=${dir}temp/;
clean=$(pwd)/cleaned;
cd $dir;
mkdir -p cleaned;
mkdir -p temp;
for z in *.cb*; do 
  cd $dir;
  filename=$(basename "$z");
  rawname="${filename%.*}";
  name="${rawname// /_}";
  7z e "$dir$z" -otemp/;
  cd $tmp && zip -r $clean/$name.cbz *.jpg;
  cd $tmp && rm *.*;
done
cd $dir && rm -rf $tmp;

I should probably be using the os /tmp dir, but I'm still new at bash and couldn't get it to work. Any suggestions to optimizing it?

share|improve this question
    
@Scott Good point, I really misunderstood the OP's request, thanks for pointing it out to me. –  MariusMatutiae Dec 2 '13 at 21:39

1 Answer 1

Your little script may work if all the filenames are unique. Will you have any conditions like this?

original.zip
- /foo/bar1.jpg 
- /foo/bar2.jpg
- /zoo/bar1.jpg
- /zoo/bar2.jpg

You are going to be overwriting files. A better solution maybe to put the full path as part of your new filename. Ex:

foobar1.jpg foobar2.jpg zoobar1.jpg zoobar2.jpg

But with this, you will loose the original filename.

Just some thoughts...

share|improve this answer
    
Not a bad suggestion! Thanx! I have no control over the file names in the archives. They come from different sources. –  xero Dec 2 '13 at 21:51

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.