Category Archives: Linux

Using Linux Tail to Trim Your Files

This tip is for my friend Scarb. For years I’ve used the Linux command “tail -f” for watching logs… but only recently did I discover its versatility for extracting parts of files.

Need to grab only the last 25 lines of a file?

tail -n 25 somefile.txt

How about the last 200000 bytes redirected to another file?

tail -c 200000 somefile.txt > newfile.txt

Handbrake batch conversion using CLI shell script

I contributed to a Handbrake forum a while back. Thought you might find it useful too.

This is my evolving script for transcoding DVDs from MacTheRipper.

1) Save the file in a logical place (I keep it in the Movies Folder… same place I rip movies to). It will loop through all titles longer than X.

#!/bin/sh
# usage is ./thisScript.sh minimumDuration SomeDirContainingVIDEO_TS
# duration is set in minutes ("30" equals 30 minutes)
# specify the container directory for the VIDEO_TS directory
# example ./handbrake-longer-than.sh 30 24_SEASON6_D1
#set default path to HandBrakeCLI
PathToHandBrakeCLI=/some/path/to/HandBrakeCLI
if [ "${1}x" = "x" ]; then
echo "Minutes not provided"
exit
fi
if [ "${2}x" = "x" ]; then
echo "VIDEO_TS path not provided"
exit
fi
time=$1
export time
for i in $(find $2 -type d -name VIDEO_TS) ; do
for title in $($PathToHandBrakeCLI -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 > (60 * ENVIRON["time"]) { print $3 "-" $5 } ') ; do
#this names the title for the output file
titlenum=$(echo $title | cut -f 2 -d '-')
# you can change the preset or any other variables here
$PathToHandBrakeCLI -i $i -o ${2}-title${titlenum}-appletv.m4v --preset="AppleTV" -2 -T -P -t ${titlenum}
#output example: 24_SEASON6_D1-title1-appletv.m4v
done
 done

2) Within the script, set the path to your HandBrakeCLI app on line 9.

3) Don’t forget to run this before trying to execute it:
chmod 777 ./handbrake-longer-than.sh

4) Execute it like this:
./handbrake-longer-than.sh 30 24_SEASON6_D1

Where “30” is the shortest title you want to export and “24_SEASON6_D1” is the name of the folder containing your VIDEO_TS directory.

Change Runtime Settings for Linux Servers

I install RHEL on all my production machines and CentOS on all my test/development boxes. Installing the GUI is helpful for configuration, but you sure don’t want to run them that way all the time. So after everything is installed, configured and tested, I make the following change:

nano /etc/inittab

Change

id:5:initdefault:

To

id:3:initdefault:

Save the file and reboot the machine. When you want to launch a GUI, just run:

startx

Add a Virtual IP Address in Linux

nano /etc/sysconfig/network-scripts/ifcfg-eth0

Should look something like this


DEVICE=eth0
BOOTPROTO=static
BROADCAST=172.16.0.255
HWADDR=00:00:00:00:00:00
IPADDR=172.16.0.2
NETMASK=255.255.255.0
NETWORK=172.16.0.0
ONBOOT=yes

Duplicate that and open the new file

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
nano /etc/sysconfig/network-scripts/ifcfg-eth0:0

Change device name, set new IP, remove reference to hardware


DEVICE=eth0:0
BOOTPROTO=static
BROADCAST=172.16.0.255
IPADDR=172.16.0.3
NETMASK=255.255.255.0
NETWORK=172.16.0.0
ONBOOT=yes

Use cURL instead of wget on Mac OS X

I use wget all the time on my Linux machines, but was surprised to find it missing on my MacBook Pro. Since Mac OS is based on BSD, it uses cURL instead. Here’s a good way to scrape files from a remote server.

curl -O http://www.gutenberg.org/files/21171/mp3/21171-[01-24].mp3

Man page is available at: http://curl.haxx.se/docs/manpage.html