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

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

Wednesday, September 21, 2005

Strip crtl-M characters from files on the command line (recurses)

find . -name '*.html' | xargs perl -pi -e 's/\r//g;'

Friday, July 15, 2005

File images in a horizontal or vertical subdirectory - uses ImageMagick

#!/usr/bin/perl

# check the image size and move to a horizontal
# or vertical subdirectory
use Image::Size;
use File::Copy;
use strict;

my $dir = "/home/rory/Jobs/images/cig_images/";
my $horiz = $dir . "horizontal";
my $vert = $dir . "vertical";

# read the files
opendir(DIR, $dir) || die "can't opendir $dir: $!";
my @images = readdir(DIR);
closedir(DIR);

# loop thru images
foreach my $image (@images) {
    my $fullpath = $dir . $image;
    if (not(-d $fullpath)) {    # exclude directories
       
        # check what dimension of the image is longer
        my ($x, $y) = imgsize("$fullpath");
        if ($x>$y) {
            move("$fullpath", "$horiz/$image") || die "move failed: $!";
        } else {
            move("$fullpath", "$vert/$image") || die "move failed: $!";   
        }
        print "$fullpath $x $y\n";
       
    } # end if for not a directory
   
} # end foreach loop thru images

Sunday, May 29, 2005

Using crypt to encrypt/decrypt files

A file can be encrypted in the shell mode using crypt, or in the edit mode using the -x or X option. When you are ready to decrypt the file, you can use the crypt command in the shell mode. The following is the command format to encrypt a file:
crypt < oldfile > newfile
The system prompts you for a password.



Before removing the unencrypted oldfile, make sure the encrypted newfile can be decrypted using the appropriate password.



To decrypt a file, redirect the encrypted file to a new file you can read. The command to decrypt a file is as follows:
crypt < encrypted_file > new_filename 

Monday, May 23, 2005

Use wget to retrieve a directory of mp3s

wget -r -l1 --no-parent -A.mp3 -R.html,.gif http://www.mysite.com/mp3/

Monday, May 9, 2005

Block a particular IP address with iptables

iptables -I INPUT -s 25.25.25.25 -j DROP

Thursday, March 3, 2005

In java, take a string and strip alphanumeric characters

Call it like this:
String newString = reformat(categoryName);
// for stripping strings of non alphanumeric stuff and replacing with +
// should really be a class

public String reformat(String categoryName)
{
StringBuffer reformattedString = new StringBuffer();
if (categoryName != null)
{
char[] chars = categoryName.toCharArray();
for (int ii = 0; ii < chars.length; ii++)
{
if (Character.isLetterOrDigit(chars[ii]))
{
reformattedString.append(chars[ii]);
}
else if (Character.isSpaceChar(chars[ii]))
{
if (reformattedString.length() > 0 && reformattedString.charAt(reformattedString.length() - 1) != '_')
{
reformattedString.append('_');
}
}
}
}
return reformattedString.toString();
}

Wednesday, March 2, 2005

Display last n lines of a file

tail -n /path/to/file

Tuesday, March 1, 2005

Parse IP from ifconfig with sed (excluding your internal IP)

/sbin/ifconfig | sed -n -e "s/^.*addr://g" -n -e "s/ Bcast.*//p" | grep -v '192.168.0.20'

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 {
        compress
        daily
        rotate 7
        notifempty
}

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]%'