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


# 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);

# 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

Monday, May 9, 2005

Block a particular IP address with iptables

iptables -I INPUT -s -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]))
else if (Character.isSpaceChar(chars[ii]))
if (reformattedString.length() > 0 && reformattedString.charAt(reformattedString.length() - 1) != '_')
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 ''