nix, shell, perl, php, mysql and mac os x tips and tricks

Friday, December 10, 2004

Set up file rotation on debian

Stick a file in /etc/logrotate.d. Call it "mylog"
or something and put this inside:
/var/mail/mylog {
        rotate 7

Monday, October 18, 2004

Some cool MySQL date manipulation stuff

SELECT unix_timestamp( curdate(  )  )  AS TODAY, unix_timestamp( DATE_SUB( curdate(  ) ,  INTERVAL 1 YEAR )  )  AS LASTYEAR, unix_timestamp("2005-10-01")  AS THISQUARTER, unix_timestamp(DATE_SUB("2005-10-01",  INTERVAL 1 YEAR) )  AS LASTQUARTER

SELECT (unix_timestamp("2005-11-15") - unix_timestamp("2005-10-01")) / 86400

SELECT  * FROM  mail_stats WHERE DATE_SUB(DATE_FORMAT( complete_date,  "%Y-%m-%d"  ),INTERVAL 1 DAY)  =  "2005-11-09"

Tuesday, September 28, 2004

VB to strip carriage returns from cells in an Excel file

Sub CleanUp()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange
   With TheCell
   If .HasFormula = False Then
       .Value = Application.WorksheetFunction.Clean(.Value)
   End If
   End With
Next TheCell
End Sub

Monday, September 27, 2004

Burn a CD from cue/bin files on the command line

cdrdao write --driver generic-mmc --device 1,2,0 --speed 12 file.cue

Monday, August 30, 2004

Update the ports sources on FreeBSD

sudo cvsup -g -L 2 /usr/share/examples/cvsup/ports-supfile.good

Friday, August 20, 2004

"save down" an mp3 to 64kpbs (warning, this overwrites the original mp3)

for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; lame -b 64 `basename $i .mp3`.wav $i; rm -f `basename $i .mp3`.wav; done

Burn a directory to CD (data)

mkisofs -r -J -v -V $(echo "$1" | cut -c 0-32) $1 | cdrecord -v -dev=0,0,0 fs=6m -eject -

Sunday, August 1, 2004

Encode all .wav files in a directory (at 96), and delete them

for i in *.mp3; do lame -b 96 $i `basename $i .wav`.mp3; rm -f $i; done

Start the gdm on debian

/etc/init.d/gdm start

MySQL query for selecting records that originated at a non-paid referer

SELECT websource, referer_host, referer_keywords, my_referer
FROM orders
WHERE referer_host <> ''
AND websource IS NULL
Use this to exclude people searching for you specifically
SELECT websource, referer_host, referer_keywords, my_referer
FROM orders
WHERE referer_host <> ''
AND websource IS NULL
AND referer_keywords NOT LIKE '%[domain]%'

Thursday, July 1, 2004

Find all files starting in the home directory that have not been accessed in the last 30 days

Note that if you run this command a second time there will be no files found because the find command accesses each file in the home directory when it runs.
find ~ -atime +30 -print

Find files edited in last day, recursively

find . -name '*.*' -mtime -1 -print

Re-start MySQL server on FreeBSD

/usr/local/etc/rc.d/  { start | stop }

Tuesday, June 1, 2004

Mount a windows drive in linux (must be root)

mount -t smbfs -o username=administrator //server/incoming /home/user/server

can use this as a normal user:

smbmount //server/incoming /home/user/server -o username=administrator

Saturday, May 29, 2004

Process an AVI output file from capture_video (MJPEG) into a DVD compatable MPEG file

# Inputs:
# 1. Input file basename (input will be filename.avi, output will be filename.mpeg)

lav2wav +p $1.avi | mp2enc -r 48000 -b 384 -o $1.mp2

lav2yuv +p $1.avi | mpeg2enc -b 3800 -q 1 -n n -f 8 -s -r 16 -v 0 -o $1.m1v

mplex -f 8 $1.mp2 $1.m1v -o $1.mpeg

rm $1.m1v
rm $1.mp2

Burn a DVD from a set of MPEG files

# Input:
# 1-255: MPEG files to be chapters of DVD

dvddirgen -o tmp_dvd -r
dvdauthor -o tmp_dvd $argv
dvdauthor -o tmp_dvd -T

dvd+rw-format -force /dev/cdrom
dvd+rw-booktype -dvd-rom-spec -unit+rw /dev/cdrom

growisofs -speed=1 -dvd-compat -overburn -Z /dev/cdrom -R -udf -dvd-video tmp_dvd

rm -rf tmp_dvd

Capture X minutes of video into file Y from /dev/video0 for DVD creation

# Arguments:
# 1. Minutes of video to burn
# 2. Base filename output will be filename.avi

system("streamer -n ntsc -t " . ($ARGV[0] * 30 * 60) . " -s 720x480 -r 30 -o $ARGV[1].avi -f mjpeg -R 48000 -F stereo -c /dev/video0");

print $ARGV[0] . " minutes captured into $ARGV[1].avi\n";

Thursday, April 1, 2004

Split a string into two variable in a shell script (uses a pipe as delimiter)

echo "var1|var2" | awk -F\| '{print $1, $2}'

Monday, February 16, 2004

Find text within files with a certain extension

find . -name '*.jsp' -exec grep "my_text" '{}' \; -print

Thursday, January 1, 2004

Encode a directory of WAVs into MP3s

for i in *.wav; do lame -b 128 $i `basename $i .wav`.mp3; done