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

Saturday, December 22, 2007

Rotate a logfile in real time

1.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

1.wget --wait=5 --limit-rate=200K -m -k -E -U 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36' --no-check-certificate https://www.47thmain.com/

Monday, September 10, 2007

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

1.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

1.^(.{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.
1.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

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

Sunday, April 22, 2007

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

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

Monday, April 16, 2007

Shell script to ecrypt/decrypt files with GPG

001.#!/bin/bash
002. 
003.# encrypts or decrypts files or directories with GPG
004.ACTION="";
005.FILEORDIR="";
006.FILE="";
007.OUTPUTFILE="${HOME}/gpgout";
008.MYOUTFILE="";
009.RECIPIENT="";
010.PASSWORD="";
011.FILEBASE="";
012.TEMPFILE="";
013.OUTFILE="";
014.GPGOUT="";
015. 
016.# ask if this is an encrypt or a decrypt
017.echo "encrypt or decrypt? (encrypt) : ";
018.read ACTION;
019.if [ -z $ACTION ]; then
020.    ACTION=encrypt;
021.fi
022. 
023.# ask if this is a file or a directory
024.echo "is this a file or directory? (file) : ";
025.read FILEORDIR;
026.if [ -z $FILEORDIR ]; then
027.    FILEORDIR=file;
028.fi
029. 
030.# ask for full path to the file
031.echo "enter full path to the $FILEORDIR (no trailing slash) : ";
032.read FILE;
033.if [ -z $FILE ]; then
034.    echo "no $FILEORDIR entered, exiting.";
035.    exit;
036.fi
037. 
038.# if directory, check it exists
039.if [ $FILEORDIR = 'directory' ]; then
040.    if [ -d $FILE ]; then
041.            echo "directory $FILE exists..."
042.        else
043.            echo "$FILE does not exist, exiting."
044.            exit;
045.    fi
046.fi
047. 
048.# if file, check it exists
049.if [ $FILEORDIR = 'file' ]; then
050.    if [ -f $FILE ]; then
051.            echo "file $FILE exists...";
052.            FILEBASE=`basename $FILE`;
053.        else
054.            echo "$FILE does not exist, exiting.";
055.            exit;
056.    fi
057.fi
058. 
059.# ask for output location
060.echo "Enter output directory with no trailing slash ($OUTPUTFILE) :";
061.read MYOUTFILE;
062.if [ -z $MYOUTFILE ]; then
063.    MYOUTFILE=$OUTPUTFILE;
064.fi
065. 
066.# check that the output dir exists, if not, create it
067.if [ -d $MYOUTFILE ];
068.    then
069.        echo "";
070.    else
071.        mkdir $MYOUTFILE;
072.fi
073. 
074.# ask for the gpg recipient
075.echo "Enter the GPG recipient :";
076.read RECIPIENT;
077.if [ -z $RECIPIENT ]; then
078.    echo "no GPG recipient entered, exiting.";
079.    exit;
080.fi
081. 
082.if [ $ACTION = 'decrypt' ]
083.    then
084.    
085.    # Ask for their GPG passphrase silently
086.    echo "Enter GPG passphrase : ";
087.    stty -echo
088.    read PASSWORD;
089.    stty echo
090. 
091.fi
092. 
093.############# BEGIN MEAT ###############
094. 
095.# if it's an encrypt job
096.if [ $ACTION = 'encrypt' ]; then
097.    
098.    if [ $FILEORDIR = 'file' ];
099.    
100.        then # if it's a file
101.            gpg --encrypt --recipient "$RECIPIENT" --output "${MYOUTFILE}/${FILEBASE}.gpg" $FILE;
102.        else # it's a directory - loop thru it and encrypt each file
103.            for file in `ls $FILE | tr : " "`
104.            do
105.                gpg --encrypt --recipient "$RECIPIENT" --output "${MYOUTFILE}/${file}.gpg" ${FILE}/${file};
106.            done
107.        
108.    fi # end if for file or directory
109. 
110.fi # end action=encrypt
111. 
112.# if it's an decrypt job
113.if [ $ACTION = 'decrypt' ]; then
114.    
115.    if [ $FILEORDIR = 'file' ];
116.    
117.        then # if it's a file
118.            OUTFILE=`echo $FILEBASE | sed 's/\.gpg//g'`;
119.            GPGOUT=`gpg --decrypt --recipient "$RECIPIENT" --output ${MYOUTFILE}/${OUTFILE} --passphrase "$PASSWORD" $FILE &> /dev/null`;
120.        else # it's a directory - loop thru it and encrypt each file
121.            for file in `ls $FILE | tr : " "`
122.            do
123.                OUTFILE=`echo $file | sed 's/\.gpg//g'`;
124.                GPGOUT=`gpg --decrypt --recipient "$RECIPIENT" --output ${MYOUTFILE}/${OUTFILE} --passphrase "$PASSWORD" ${FILE}/${file} &> /dev/null`;
125.            done
126.        
127.    fi # end if for file or directory
128.    
129.    echo "";
130.    echo "BE SURE TO DELETE THE DECRYPTED FILES!";
131. 
132.fi # end action=decrypt

Thursday, January 25, 2007

Sync date/time on linux with ntpdate

1.ntpdate ntp.netsville.com

Tuesday, January 16, 2007

Find files base on attributes

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

Friday, January 12, 2007