Archive for the ‘ shell scripting ’ Category

Script to run handbrake recursively through a folder tree

is a fantastic tool for (among other uses), converting videos into mobile formats. It is extremely easy to use and can usually get a video properly converted in just a few clicks. To get Videos encoded for use on my phone, I use the “iPhone & iPod Touch” preset. It will convert your video to .mp4 as well as scale it down to a consumable size. This format will work on any smartphone I’ve seen.

My issue is that when converting a folder full of videos, I don’t want to have to use the gui to add a bunch of videos to the queue one by one. Its a very clicky process. This simple will walk handbrake through all the files in a selected folder (and its subfolders). This uses the find command to traverse recursively through a directory structure. It will place the transcoded file in the same folder as its source and change its extension to “.mp4″.

Usage: handbrakefolder.sh [FOLDER]
Run handbrake on all the files contained in [FOLDER]. (the current directory by default)

#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running: "HandBrakeCLI --preset-list"
#
PRESET="iPhone & iPod Touch"
if [ -z "$1" ] ; then
	TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%\.*}".mp4 --preset="$PRESET"' __ {} \;

Save that into a .sh file like “handbrakefolder.sh” and grant it the execute permission (chmod +x handbrakefolder.sh).

Thanks to Vinnie and http://mywiki.wooledge.org/UsingFind for their help in this.

Now you can simply execute this against a folder containing your . like this:


For a folder structure like this:
Videos
-> show1
    vid1.avi
-> show2
    -> season 1
        ep1.avi
        ep2.avi
    -> season 2
        ep1.avi
        ep2.avi
-> show3
    vid1.avi
    vid2.avi

hostname% handbrakefolder.sh Videos
...
....
...
When done, it will look like this:
Videos
-> show1
    vid1.avi
    vid1.mp4
-> show2
    -> season 1
        ep1.avi
        ep1.mp4
        ep2.avi
        ep2.mp4
    -> season 2
        ep1.avi
        ep1.mp4
        ep2.avi
        ep2.mp4
-> show3
    vid1.avi
    vid1.mp4
    vid2.avi
    vid2.mp4

Write zero’s to disk Infinite loop

I was bored… So this is a little bash i wrote that will write zero’s to a hard disk infinately until you press CTRL+C. It uses dcfldd instead of plain because i like the progress output it provides.

#!/bin/bash
echo -e "Which disk would you like to wipe out? (sda, sdb, sdc)?   \c"
read DISK
read -p "You picked "$DISK", are you sure? (y/n)" -n 1
if [[ $REPLY =~ ^[Yy]$ ]]
then
C=1
for (( ; ; ))
do
        echo -e "\nStarting zero Sweep number "$C" ..."
        dcfldd if=/dev/zero of=/dev/$DISK
        echo "Zero's written to Disk "$C" time(s)"
        echo "[ hit CTRL+C to stop ]"
        let C=C+1
done
fi

Script to run handbrake on an entire folder

UPDATED 8-10-10:
This has been superceeded by its newer version at http://www.surlyjake.com/2010/08/script-to-run-handbrake-recursively-through-a-folder-tree/. The new version features all of the previous features, but can also traverse recursively through a folder structure.

#!/bin/bash
if [ -z "$1" ] ; then
	TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
for file in "$TRANSCODEDIR"/*
do
	HandBrakeCLI -i "${file}" -o "${file}.mp4" --preset="iPhone & iPod Touch"""
done

Save that into a .sh file like “handbrakefolder.sh”

script to update wordpress

Why? Because i’m lazy. I’ll this to see if it works
#!/bin/sh
#your current site will be backed up to your home folder with the date in the name
#change this to whatever you want
BACKUPNAME="_site_b4_update"
BACKUPDATE=`date +%y%m%d`
#this is the path to your wordpress site's root
SITEROOT="/usr/local/www/"
#here she goez...
cd ~
tar -pczf "$BACKUPNAME""$BACKUPDATE".tar.gz "$SITEROOT"
cd /usr/local
fetch http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
rm latest.tar.gz
cp -R wordpress/* "$SITEROOT"/
rm -R wordpress
chown -R www "$SITEROOT"