Tag Archives: Lt

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