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

Monday, November 17, 2003

Burn a disc-at-once CD from the command line

First create a .toc file with reference to your wav's as such:
CD_DA

TRACK AUDIO
AUDIOFILE "01.wav" 0

TRACK AUDIO
AUDIOFILE "02.wav" 0

TRACK AUDIO
AUDIOFILE "03.wav" 0

TRACK AUDIO
AUDIOFILE "04.wav" 0

then invoke cdrdao as such:
cdrdao write --driver generic-mmc cdtmpdir.toc

Sunday, November 16, 2003

Verbose list of details of running processes

ps auxwww

Saturday, November 15, 2003

Recursively delete a filename in a filesystem...quite dangerous

find /path/to/start -name "filename" -exec rm {} \;

Re-compile mandrake kernel

There are some tutorials out there and there is one provided with your
documents, but if you just want to recompile your current kernel it is so
easy, that I'm gpoing to tell you only what you need in order to have it run
1. Install the kernel-source package(with softwaremanager) if not already done

2. Open a konsole as root and type:

cd /usr/src/linux

3. cleanup the directory with command:

make mrproper

3. Configure the kernel:

make xconfigure

4. Now activate ACPI in the section General Setup, activate all modules if
possibvle mark with "y" y means to built into kernel, not as module. Don't
change anything else in the kernel config otherwise you have to do some more
things that I don't explain now.

5. Save and exit

6. Compile:

make dep && make bzImage && make modules && make modules_install

After that is done do the following:

cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-2.4.19-acpi

cp /usr/src/linux/System.map /boot/System.map-2.4.19-acpi

7. Now you just need to either use drakboot to add that kernel to lilo.conf or
just add the lines for this kernel to lilo.conf manually with an editor of
your choice

for example you would add:

image=/boot/vmlinuz-2.4.19-acpi
label=2.4.19-acpi
root=/dev/hda7
initrd=/boot/initrd-2.4.19-16mdk.img
append="noquiet devfs=mount hdc=ide-scsi"
vga=791
read-only

8. Now you rerun lilo with command, as root:

/sbin/lilo

9. Don't forget to install the acpid script with command:

urpmi acpid

you can also do that with softwaremanager(rpmdrake)

10. Run command:

drakxservices

disable the apm daemon and enable acpid

11. Reboot select the kernel mared as 2.4.19-acpi test if everything works
fine if it does you can make it your default kernel.

Thursday, November 13, 2003

Check for, and count duplicate records in MySQL

SELECT field, count( field )
FROM table
GROUP BY field
HAVING Count( field ) > 1 LIMIT 0 , 30

Sorted list of directory sizes

NOTE: does not show hidden files or directories
du -s * | sort -n

Saturday, November 8, 2003

Count within a "for" loop in shell script

for ((  i = 0 ;  i <= 5;  i++  ))
do
  echo "Welcome $i times"
done

Friday, November 7, 2003

Saturday, October 25, 2003

recurse thru a category structure and count items in categories with perl and mysql

Loop thru the categories table (must have category_code AND category_parent),
and inside the loop call the subroutine. sub db_query is just a simple query wrapper.
# loop thru the categories and count all prods underneath
my $prodcount=0;
my $cat_loop_q = "SELECT category_code,category_parent FROM $categories";
my $sth = &db_query($cat_loop_q);
while (my $ref = $sth->fetchrow_arrayref) {
 $prodcount = &countTree($$ref[0]);
 my $update_q = "UPDATE $categories SET count = '$prodcount' WHERE category_code = '$$ref[0]'";
 my $update = &db_query($update_q);
 $prodcount=0;
}
sub countTree {
 my ($root) = shift; 
 my $drill_q = "SELECT category_code FROM $categories WHERE category_parent = '$root'";
 #print "$drill_q\n";
 my $sth = &db_query($drill_q);
 my $rc = $sth->rows;
 if ($rc>0) {
  while (my $ref = $sth->fetchrow_arrayref) {
   &countTree($$ref[0]);
  }
 } 
 my $sql_q = "SELECT 
  a.category_name,
  a.category_code,
  a.category_parent,
  COUNT(DISTINCT b.skuid) 
  FROM $categories AS a, $products_to_categories AS b 
  WHERE a.category_code=b.category_code 
  AND a.category_code='$root'
  GROUP BY a.id";
  
 my $sth = &db_query($sql_q);
 my ($name,$parent,$code,$count);
 while (my $ref = $sth->fetchrow_arrayref) {
  $name=$$ref[0];
  $code=$$ref[1];
  $parent=$$ref[2];
  $count=$$ref[3];
  $prodcount=$prodcount+$count;
  
 }
 return($prodcount);
 
}

Friday, October 24, 2003

MySQL to show an entire category tree

SELECT a.category_name AS parent_name,
b.category_name AS child_name, 
a.category_code as a_category_code, 
a.category_parent AS a_category_parent, 
b.category_code as b_category_code, 
b.category_parent AS b_category_parent
FROM categories_raw_loop AS a, categories_raw_loop AS b 
WHERE a.category_code = b.category_parent
ORDER BY a.category_code;

Wednesday, October 22, 2003

MySQL Load data local infile

LOAD DATA LOCAL INFILE "/full/path/file" INTO TABLE [table] FIELDS TERMINATED BY ';' (col1,col2);

Select most popular items in MySQL

SELECT count( unoptioned_skuid ) as count , unoptioned_skuid as skuid
FROM orders_products
GROUP  BY unoptioned_skuid 
ORDER BY count DESC

Tuesday, October 21, 2003

Burn an ISO to CD from command line

cdrecord -v -pad speed=1 dev=1,2,0 src.iso  

Monday, October 20, 2003

Fun with tar and gzip

recursively backup files and directories, excluding some:
tar -czvf /home/site/backup.tar.gz /home/site/cgi-bin --exclude=mysubdir

append more files/dirs to an existing archive:

tar -r --file=/home/site/backup.tar /home/site/more_stuff

don't forget to gzip it when you're done!

gzip /home/site/backup.tar

list the contents of a tar.gz


gzip -dc /home/site/backup.tar.gz | tar tvf -

Find installed perl modules

find `perl -e 'print "@INC"'` -name '*.pm' -print

Get the size of a file within a shell script

file_size=$(ls -l "path/file" | awk '{ print $5 }')
echo $file_size

Create a .tgz archive

tar -czf [archive_name].tgz *

Saturday, October 18, 2003

Search and replace text in files from the command line

find . -name '*.html' -maxdepth 1 | xargs perl -pi -e 's/search_text/replace_text/g;'

Create CDs from XMMS playlists

#! /bin/bash
#
# M3U CDWRITER
# 10/16/2003
# rory o'connor - rory | thewhiteroom.com
#
# writes CDs from .m3u files created with XMMS
# Tracks are written in the order they appear in
# the .m3u file.
#
# prerequisites:
# lame
# normalize
# cdrecord
# m3u playlist no longer than 1:19 (for 80min cd)
#
######configuration
# "dev" parameter to cdrecord.  if you don't know,
# try 'cdrecord --scanbus' on the cmd line
dev=1,2,0
#
# speed of the cdrecorder
speed=12
#
# the temp directory for .wav files
# THIS SCRIPT CREATES AND REMOVES IT
holding=$HOME/cdtmpdir # no trailing slash

file=""
m3u=""
zero=0
count=0

if [ -d $holding ]; then echo "CD temp ($holding) cannot already exist...exiting."; exit; fi

echo "Please enter the path and filename of the .m3u file you want to write to CD relative to $HOME:"
read m3u

file=$HOME/$m3u

if [ -f $file ]

then
 
 # make the temp directory
 mkdir $holding
 
 for file in $file
 do
  grep -v '^#' $file | while read line
  do
 
  count=$[ $count + 1 ] 
  name=`echo $line | awk -F"*" '{print $1}'` 
  echo "$name" 
  
  # check the length of $count
  length=`echo "$count" | wc -c | cut -c1-8`
  length=$[ $length - 1 ] 
  
  # fix the count to a 2-digit number
  if [ $length = 1 ]; then
   newcount=$zero$count
  else
   newcount=$count
  fi
  
  # write wav file to our holding pen
  lame --decode "$name" $holding/$newcount.wav
  
  done
 
 done

  
 # normalize the wav files
 normalize -m $holding/*.wav
 
 # burn the CD
 cdrecord dev=$dev -eject speed=$speed -pad -audio $holding/*.wav
 
 # remove the files
 
 for file in `ls -1 $holding`
 do 
  rm -f $holding/$file
 done
 
 rmdir $holding
 
else 

 echo "file does not exist."

fi