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

Saturday, December 22, 2007

Rotate a logfile in real time

while(true); do wc -l 92_OQ1_maillog.txt; mv 92_OQ1_maillog.txt 92_OQ1_maillog.txt.`echo $(date '+%m%d%y%N')`; sleep 5400; done;

Monday, December 10, 2007

Monday, September 24, 2007

Mirror an entire site, dynamic or not, using wget

wget --wait=20 --limit-rate=20K -m -k -E -U 'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20041001' http://www.site.com/

Monday, September 10, 2007

Update column in a table whose values are NOT found in another table

UPDATE products_staging LEFT JOIN published ON products_staging.SKUID=published.skuid SET products_staging.PUBLISH = '0' WHERE published.skuid IS NULL

Tuesday, July 24, 2007

regex to grab the first n characters of a string, respecting words

^(.{0,MAXCHAR}[^ ,.]*)(.*)

Monday, July 23, 2007

Shell script to re-name first part of filename to uppercase, and the extension to lowercase

For example ck100.Jpg -> CK100.jpg. Also removes spaces. Must be run in the directory the files are located in.
for file in *; do myfile=`echo $file | sed -e 's/ //g'`; first=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\1/'|tr '[a-z]' '[A-Z]'`; ext=`echo $myfile | sed 's/^\(.*\)\.\(.*\)/\2/'|tr '[A-Z]' '[a-z]'`;  mv "$file" `echo "$first.$ext"`; done

Monday, May 28, 2007

regular expression to parse country code out of whois output

^[Cc]ountry.+:.+([A-Z]{2}).+$

Sunday, April 22, 2007

copy a list of matches from a find command to another directory

find . -name "*.html" -mtime -35 -exec cp {} ~/Sites/site/cat/templates/includes/ \;

Monday, April 16, 2007

Shell script to ecrypt/decrypt files with GPG

#!/bin/bash

# encrypts or decrypts files or directories with GPG
ACTION="";
FILEORDIR="";
FILE="";
OUTPUTFILE="${HOME}/gpgout";
MYOUTFILE="";
RECIPIENT="";
PASSWORD="";
FILEBASE="";
TEMPFILE="";
OUTFILE="";
GPGOUT="";

# ask if this is an encrypt or a decrypt
echo "encrypt or decrypt? (encrypt) : ";
read ACTION;
if [ -z $ACTION ]; then
    ACTION=encrypt;
fi

# ask if this is a file or a directory
echo "is this a file or directory? (file) : ";
read FILEORDIR;
if [ -z $FILEORDIR ]; then
    FILEORDIR=file;
fi

# ask for full path to the file
echo "enter full path to the $FILEORDIR (no trailing slash) : ";
read FILE;
if [ -z $FILE ]; then
    echo "no $FILEORDIR entered, exiting.";
    exit;
fi

# if directory, check it exists
if [ $FILEORDIR = 'directory' ]; then
    if [ -d $FILE ]; then
            echo "directory $FILE exists..."
        else
            echo "$FILE does not exist, exiting."
            exit;
    fi
fi

# if file, check it exists
if [ $FILEORDIR = 'file' ]; then
    if [ -f $FILE ]; then
            echo "file $FILE exists...";
            FILEBASE=`basename $FILE`;
        else
            echo "$FILE does not exist, exiting.";
            exit;
    fi
fi

# ask for output location
echo "Enter output directory with no trailing slash ($OUTPUTFILE) :";
read MYOUTFILE;
if [ -z $MYOUTFILE ]; then
    MYOUTFILE=$OUTPUTFILE;
fi

# check that the output dir exists, if not, create it
if [ -d $MYOUTFILE ];
    then
        echo "";
    else
        mkdir $MYOUTFILE;
fi

# ask for the gpg recipient
echo "Enter the GPG recipient :";
read RECIPIENT;
if [ -z $RECIPIENT ]; then
    echo "no GPG recipient entered, exiting.";
    exit;
fi

if [ $ACTION = 'decrypt' ]
    then
   
    # Ask for their GPG passphrase silently
    echo "Enter GPG passphrase : ";
    stty -echo
    read PASSWORD;
    stty echo

fi

############# BEGIN MEAT ###############

# if it's an encrypt job
if [ $ACTION = 'encrypt' ]; then
   
    if [ $FILEORDIR = 'file' ];
   
        then # if it's a file
            gpg --encrypt --recipient "$RECIPIENT" --output "${MYOUTFILE}/${FILEBASE}.gpg" $FILE;
        else # it's a directory - loop thru it and encrypt each file
            for file in `ls $FILE | tr : " "`
            do
                gpg --encrypt --recipient "$RECIPIENT" --output "${MYOUTFILE}/${file}.gpg" ${FILE}/${file};
            done
       
    fi # end if for file or directory

fi # end action=encrypt

# if it's an decrypt job
if [ $ACTION = 'decrypt' ]; then
   
    if [ $FILEORDIR = 'file' ];
   
        then # if it's a file
            OUTFILE=`echo $FILEBASE | sed 's/\.gpg//g'`;
            GPGOUT=`gpg --decrypt --recipient "$RECIPIENT" --output ${MYOUTFILE}/${OUTFILE} --passphrase "$PASSWORD" $FILE &> /dev/null`;
        else # it's a directory - loop thru it and encrypt each file
            for file in `ls $FILE | tr : " "`
            do
                OUTFILE=`echo $file | sed 's/\.gpg//g'`;
                GPGOUT=`gpg --decrypt --recipient "$RECIPIENT" --output ${MYOUTFILE}/${OUTFILE} --passphrase "$PASSWORD" ${FILE}/${file} &> /dev/null`;
            done
       
    fi # end if for file or directory
   
    echo "";
    echo "BE SURE TO DELETE THE DECRYPTED FILES!";

fi # end action=decrypt

Thursday, January 25, 2007

Sync date/time on linux with ntpdate

ntpdate ntp.netsville.com

Tuesday, January 16, 2007

Find files base on attributes

find / -amin -10    # find files accessed in last 10 minutes
find / -atime -2    # find files accessed in last 48 hours
find / -empty       # find empty files and directories
find / -group cat   # find files owned by group cat
find / -mmin -5     # find files modified in last 5 minutes
find / -mtime -1    # find files modified in last 24 hours
find / -nouser      # find files owned by an invalid user
find / -user fred   # find files owned by fred

Friday, January 12, 2007