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

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

Thursday, December 7, 2006

Get files named within a numerical range with curl

This used to work for getting all the This American Life Episodes. May have other uses now.
curl --limit-rate 20240 -O -A 'iTunes/4.7 (Macintosh; N; PPC)' http://wbez-tal.streamguys.us:8020/content/[1-321].mp3

Tuesday, December 5, 2006

Use javascript to search-and-replace international characters in form entry fields (and do some other stuff)

 function ak_hi_check() {

    if  ( (mainForm.shipping_state.value == "AK") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "HI") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "PR") && (mainForm.ShipVia.value != "27") ) {
        alert("You must choose Puerto Rico as your shipping method");
        return false;
    } 
  
else {
        return true;
    }
}

// searches and replaces intl characters
function mySubmit()
 {
     //replace any special characters entered into text entry fields
    document.mainForm.billing_name.value = convert(document.mainForm.billing_name.value);
    document.mainForm.billing_first_name.value = convert(document.mainForm.billing_first_name.value);
    document.mainForm.billing_last_name.value = convert(document.mainForm.billing_last_name.value);
    document.mainForm.billing_addr1.value = convert(document.mainForm.billing_addr1.value);
    document.mainForm.billing_addr2.value = convert(document.mainForm.billing_addr2.value);
    document.mainForm.billing_city.value = convert(document.mainForm.billing_city.value);
    document.mainForm.billing_zip.value = convert(document.mainForm.billing_zip.value);
    document.mainForm.billing_home_phone.value = convert(document.mainForm.billing_home_phone.value);
    document.mainForm.shipping_name.value = convert(document.mainForm.shipping_name.value);
    document.mainForm.shipping_first_name.value = convert(document.mainForm.shipping_first_name.value);
    document.mainForm.shipping_last_name.value = convert(document.mainForm.shipping_last_name.value);
    document.mainForm.shipping_addr1.value = convert(document.mainForm.shipping_addr1.value);
    document.mainForm.shipping_addr2.value = convert(document.mainForm.shipping_addr2.value);
    document.mainForm.shipping_city.value = convert(document.mainForm.shipping_city.value);
    document.mainForm.shipping_zip.value = convert(document.mainForm.shipping_zip.value);
    document.mainForm.shipping_home_phone.value = convert(document.mainForm.shipping_home_phone.value);
    document.mainForm.po_number.value = convert(document.mainForm.po_number.value);
   
    // make sure they are choosing the correct ship method
    if  (ak_hi_check()==false) {
           return false;
    } 
    else {
        document.mainForm.action = "InvoicePreview";
        document.mainForm.submit();
    }
}

// the characters to search-and-replace
function convert(text)
{
    text = text.replace(/À/gi,"a");
    text = text.replace(/Á/gi,"a");
    text = text.replace(/Â/gi,"a");
    text = text.replace(/Ã/gi,"a");
    text = text.replace(/Ä/gi,"a");
    text = text.replace(/Å/gi,"a");
    text = text.replace(/Æ/gi,"ae");
    text = text.replace(/Ç/gi,"c");
    text = text.replace(/È/gi,"e");
    text = text.replace(/É/gi,"e");
    text = text.replace(/Ê/gi,"e");
    text = text.replace(/Ë/gi,"e");
    text = text.replace(/Ì/gi,"i");
    text = text.replace(/Í/gi,"i");
    text = text.replace(/Î/gi,"i");
    text = text.replace(/Ï/gi,"i");
    text = text.replace(/Ð/gi,"eth");
    text = text.replace(/Ñ/gi,"n");
    text = text.replace(/Ò/gi,"o");
    text = text.replace(/Ó/gi,"o");
    text = text.replace(/Ô/gi,"o");
    text = text.replace(/Õ/gi,"o");
    text = text.replace(/Ö/gi,"o");
    text = text.replace(/Ø/gi,"o");
    text = text.replace(/Ù/gi,"u");
    text = text.replace(/Ú/gi,"u");
    text = text.replace(/Û/gi,"u");
    text = text.replace(/Ü/gi,"u");
    text = text.replace(/Ý/gi,"y");
    text = text.replace(/Þ/gi,"THORN");
    text = text.replace(/ß/gi,"s");
    text = text.replace(/à/gi,"a");
    text = text.replace(/á/gi,"a");
    text = text.replace(/â/gi,"a");
    text = text.replace(/ã/gi,"a");
    text = text.replace(/ä/gi,"a");
    text = text.replace(/å/gi,"a");
    text = text.replace(/æ/gi,"ae");
    text = text.replace(/ç/gi,"c");
    text = text.replace(/è/gi,"e");
    text = text.replace(/é/gi,"e");
    text = text.replace(/ê/gi,"e");
    text = text.replace(/ë/gi,"e");
    text = text.replace(/ì/gi,"i");
    text = text.replace(/í/gi,"i");
    text = text.replace(/î/gi,"i");
    text = text.replace(/ï/gi,"i");
    text = text.replace(/ð/gi,"eth");
    text = text.replace(/ñ/gi,"n");
    text = text.replace(/ò/gi,"o");
    text = text.replace(/ó/gi,"o");
    text = text.replace(/ô/gi,"o");
    text = text.replace(/õ/gi,"o");
    text = text.replace(/ö/gi,"o");
    text = text.replace(/ø/gi,"o");
    text = text.replace(/ù/gi,"u");
    text = text.replace(/ú/gi,"u");
    text = text.replace(/û/gi,"u");
    text = text.replace(/ü/gi,"u");
    text = text.replace(/ý/gi,"y");
    text = text.replace(/þ/gi,"thorn");
    text = text.replace(/ÿ/gi,"y");
    return text;
}

Friday, December 1, 2006

Another workaround for system argument list too long error when using grep

find . -type f -maxdepth 1 |xargs grep -rHn [searchword]

Thursday, November 16, 2006

Check uptime in a continuous fashion

while(true) ; do uptime; sleep 3; done

Wednesday, September 13, 2006

This might also work for forwarding e-mail

In the ~user HOME directory, add a file called: .forward
and put inside it this line:
forward@email.com
Then set the permissions to 600.

Making mail aliases at the server level

You can create an alias in /etc/aliases (or /etc/mail/aliases) on
the server like this:
aliasuser: forward@email.com
Then run 'newaliases'.

Tuesday, June 6, 2006

Import text into MySQL from command line

mysqlimport --fields-terminated-by=, --lines-terminated-by="\n" --user=user--password DATABASE TABLE_NAME.csv

Monday, April 24, 2006

ms sql for search-and-replace based on key/value table (not really tested)

    DECLARE @word1 varchar(1000)
     DECLARE @word2 varchar(1000)
     DECLARE word_cursor CURSOR LOCAL fast_forward FOR
     SELECT old_word, new_word FROM word_replace_table

     OPEN word_cursor

     FETCH NEXT FROM word_cursor INTO @word1, @word2

 WHILE @@FETCH_STATUS = 0

 BEGIN


   SET xact_abort ON
   BEGIN tran

   DECLARE @otxt varchar(1000)

   SET @otxt = @word1 /****/
   DECLARE @ntxt varchar(1000)

   SET @ntxt = @word2 /****/

   DECLARE @txtlen int
   SET @txtlen = len(@otxt)

   DECLARE @ptr BINARY(16)
   DECLARE @pos int
   DECLARE @id int
   DECLARE curs CURSOR LOCAL fast_forward
   FOR
   SELECT

   productId,
   textptr(description),
   charindex(@otxt, description)-1
   FROM
   product
   WHERE
   description
   LIKE
   '%' + @otxt +'%'

   OPEN curs

   FETCH NEXT FROM curs INTO @id, @ptr, @pos

   WHILE @@fetch_status = 0

   BEGIN

   print 'Text found in row id=' + cast(@id AS varchar) + ' at pos=' + cast(@pos AS varchar)

   updatetext product.description @ptr @pos @txtlen @ntxt

   FETCH NEXT FROM curs INTO @id, @ptr, @pos

   END

   CLOSE curs

   DEALLOCATE curs

   commit tran

     FETCH NEXT FROM word_cursor INTO @word1, @word2
 END

     CLOSE word_cursor
     DEALLOCATE word_cursor

Friday, April 7, 2006

clean ctrl-M chars from a file (be VERY careful with this one - better to test)

cat options.csv | sed "s/[^M]$//" > options1.csv

Tuesday, April 4, 2006

Shell script to make all files in directory lowercase

#!/bin/bash
for i in $(ls); do
    oldname="$i"
    newname=$(echo "$oldname" | tr 'A-Z' 'a-z')
    if [ "$oldname" != "$newname" ]
        then
            mv -i "$oldname" "$newname"
        fi
done

Thursday, March 30, 2006

Search and replace in MSSQL (specifically in TEXT field)

Just use Ctrl+Shft+M in Query Analyzer to replace the parameters. Then hit "play" in query analyzer til u don't get any more hits.
/*
*
* Search & Replace
*
* Use Ctrl+Shift+M to replace template values
*
*/

set xact_abort on
begin tran

declare @otxt varchar(1000)
set @otxt = ''

declare @ntxt varchar(1000)
set @ntxt = ''

declare @txtlen int
set @txtlen = len(@otxt)

declare @ptr binary(16)
declare @pos int
declare @id int

declare curs cursor local fast_forward
for
select
 id,
 textptr(),
 charindex(@otxt, )-1
from
 
where
 
like
 '%' + @otxt +'%'

open curs

fetch next from curs into @id, @ptr, @pos

while @@fetch_status = 0
begin
 print 'Text found in row id=' + cast(@id as varchar) + ' at pos=' + cast(@pos as varchar)
 
 updatetext  . @ptr @pos @txtlen @ntxt

 fetch next from curs into @id, @ptr, @pos 
end

close curs
deallocate curs

commit tran

Wednesday, March 1, 2006

Workaround for "argument list too long" error on 'nix

find /home/rory/backup/Mail/outbox/ -type f -name '*' -exec cp {} /home/rory/Mail/outbox/ \;

Monday, February 27, 2006

List only directories

ls -d */

Delete files older than x days

find /home/mysite/dir/ -type f -mtime +35 -exec rm {} \;

Monday, October 17, 2005

Extract a single directory from a tar.gz file

tar -zvxf myfile.tar.gz path/in/tarfile/to/extract

Wednesday, October 5, 2005

Purge mail from exim queue

mailq | awk '{print $3}' | xargs exim  -Mrm