2014年9月8日 星期一

BASH script: resize pictures in rar archive

It's tested on CentOS 7.

Before use it, you must install zip and ImageMagick:
yum install zip ImageMagick
and unrar:
# wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# rpm -Uvh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# yum update
# yum install unrar

Create a exectable file (it names "rarresizer.sh" here.) with Code below:
#!/bin/bash
mkdir ./tmp
mkdir ./produced
IFS=$'\n'
for UNRARFILE in $( ls *.rar ); do
 echo item: $UNRARFILE
 UNRARCACHE="${UNRARFILE%.rar}"
 mkdir ./tmp/$UNRARCACHE
 unrar x $UNRARFILE tmp/$UNRARCACHE
 find ./tmp/$UNRARCACHE -name *.jpg -print -exec mogrify -resize x1308\> {} \;
 find ./tmp/$UNRARCACHE -name *.png -print -exec mogrify -resize x1308\> {} \;
 find . -name *.txt -exec rm -f {} \;
 find . -name *.url -exec rm -f {} \;
 cd ./tmp
 zip -0 -r ../produced/$UNRARCACHE.zip ./$UNRARCACHE
 cd ..
done
echo done\!
unset IFS

You can change resize parameter with width x height like this:
 find ./tmp/$UNRARCACHE -name *.jpg -print -exec mogrify -resize 512x1308 {} \;

or by percentage:
 find ./tmp/$UNRARCACHE -name *.jpg -print -exec mogrify -resize 50% {} \;

Execute: copy zipresizer.sh to the directory that you wanna to resize, and execute it use ". "
#. rarresizer.sh

Warning:
1. All rar archive files in directory will be resized.
2. Verify your permissions.
3. not use "./" like below when you execute it:
#./rarresizer.sh
4. Only jpg and png will be resize.
5. It will delete *.txt and *.url in your rar archive file.
6. You must delete "tmp" manually after resizing done.

BASH script: resize pictures in zip archive

It's tested on CentOS 7.

Before use it, you must install zip, unzip and ImageMagick:
yum install zip unzip ImageMagick

Create a exectable file (it names "zipresizer.sh" here.) with Code below:
#!/bin/bash
IFS=$'\n'
mkdir ./tmp
mkdir ./produced
for UNZIPFILE in $( ls *.zip ); do
 echo item: $UNZIPFILE
 UNZIPCACHE="${UNZIPFILE%.zip}"
 mkdir ./tmp/$UNZIPCACHE
 unzip $UNZIPFILE -d tmp/$UNZIPCACHE
 find ./tmp/$UNZIPCACHE -name *.jpg -print -exec mogrify -resize x1308\> {} \;
 find ./tmp/$UNZIPCACHE -name *.png -print -exec mogrify -resize x1308\> {} \;
 find . -name *.txt -exec rm -f {} \;
 find . -name *.url -exec rm -f {} \;
 cd ./tmp
 zip -0 -r ../produced/$UNZIPFILE ./$UNZIPCACHE
 cd ..
done
echo done\!
unset IFS

You can change resize parameter with width x height like this:
 find ./tmp/$UNZIPCACHE -name *.jpg -print -exec mogrify -resize 512x1308 {} \;
or by percentage:
 find ./tmp/$UNZIPCACHE -name *.jpg -print -exec mogrify -resize 50% {} \;

Execute: copy zipresizer.sh to the directory that you wanna to resize, and execute it use ". "
# . zipresizer.sh

Warning:
1. All zip archive files in directory will be resized.
2. Verify your permissions.
3. not use "./" like below when you execute it:
# ./zipresizer.sh
4. Only jpg and png will be resize.
5. It will delete *.txt and *.url in your zip archive file.
6. You must delete "tmp" manually after resizing done.