Batch Zip and Rename – I love the Terminal!

Today I needed to compress numerous (around 25) directories into individual archives (in this case, zip’s) and then rename them with a different extension (in this case ‘cbz’). This would have been extremely painful to do manually… I mean, right click a directory — one at a time — then left click “compress” (or zip, depending on the context menu)… and I’m not even going to go into renaming (there are at least 3 ways to manually rename a file, and I don’t want to describe them now. If you care to know just ask).

So what did I do? Did I really bother with all of that nonsense?! NO WAY! I have no patience for that!

Here’s the easy way:

1.) First, open your terminal (sorry, this is for Mac and Linux machines only, I don’t have time to convert the code to work on Windows).

2.) Next, navigate to the directory your files are in.

3.) Finally, run this command:


for dir in `ls`; do zip $dir $dir/*; mv $dir*zip $dir.cbz; done

And it’s as simple as that! All done in one easy process, just a few lines of code!

If you just want to do a batch zip, without the rename, use this code instead:


for dir in `ls`; do zip $dir $dir/*; done

I hope that helps someone out there…

Update

Here are a few different methods to try for batch-zipping in Linux:


## zip all files and folders in current directory
zip -r "${PWD##*/}.zip" *

## zip the contents of each folder from the current directory into an archive
for x in *; do if [ -d "$x" ]; then cd "$x"; zip -r "../$x.zip" *; cd ..; fi; done

## zip each folder into an archive
for x in *; do if [ -d "$x" ]; then zip -r "$x.zip" "$x"; fi; done

## another way to batch-zip
for i in $(find ./ -type d -maxdepth 1);do zip -r9 $i.zip $i; done

## same as above only using tar instead of zip
for i in $(find ./ -type d -maxdepth 1);do tar -czvf $i.tgz $i; done

Alex

Alex is a professional web and mobile developer. He loves spending time with his wife and kids, and is still very much a kid at heart. He enjoys reading comic books, watching TV/Movies, playing video games, playing with action figures and Legos.

More Posts - Website - Twitter - LinkedIn - Google Plus

Related posts:

9 thoughts on “Batch Zip and Rename – I love the Terminal!”

  1. Hi there,
    I just tried your terminal code for batch zipping and converting to CBZ format it works a treat thank you. I have run into a problem though and that it the code does not work for Folder names with paces in them eg: “chobits 001, chobits 002. . . etc” is there a way to change the code a little so that I can include spaces in the folder names?

    Feel free to talk to me like a moron this is the first time I have ever attempted to use the terminal.

    Thanks for your time

    1. Hey Norman, thanks for the comment. I’m glad the script has been working and is useful for you.
      In regards to the banes with spaces: ihavent tried it yet, but I think you would just need to enclose the name in quotes or escape the space using a slash. I’ll give it a try later when tonight and let you know.

  2. Great tip.
    Having a little trouble getting the exact result I want though.

    I want to create a .ZIP file for each album in my music directory.

    Since I have about 400 albums this would be time consuming, so I tried a BATCH method, but am not quite there.

    Each artist has a folder that contains borders of each album. Using a shell command at the ROOT level:

    for x in *; do if [ -d “$x” ]; then cd “$x”; zip -r “../$x.zip” *; cd ..; fi; done

    creates an archive of EACH ARTIST CONTAINING ONE ZIP FILE FOR ALL ENCLOSED ALBUMS.

    I need it to open EACH ARTIST FOLDER and CREATE AN ARCHIVE for EACH ALBUM.

    Even better if it CREATED AN ARCHIVE in EACH ALBUM folder, but either would do. Any ideas?

  3. Hi, late to the party… I know.
    I tried this terminal command (copied and pasted) but it threw errors at me.

    zip error: Nothing to do! ((U).ext)
    zip warning: name not matched: filename/*

    I found another way though. I already had Hazel installed, so I thought I’d make use of it.

    I added the folder, added a condition if extension is ext (or I could have gone not zip) do the following – archive. Worked a treat.

    1. Also this has the added benefit of once setup, any files added to the folder that meet the conditions will automatically be archived. I really don’t use Hazel enough.

    2. Hi @BlackDuke07,

      It’s been a while since I wrote this post and I don’t have time to test the commands right now. It’s likely you were getting errors from the copy/paste because of encoding issues. I’m guessing the database and/or backend of this site has changed the encoding and/or characters used in my original post. Your best bet would be to type it out manually (I realize that doesn’t sound like any fun).

      I’m glad you found a workaround. I have not tried Hazel, but I’m glad it worked for your needs.

      Take care,
      Alex

Leave a Reply

Your email address will not be published. Required fields are marked *