? Earlier 23 items total Later ?

On this page:?

get rid of mac's ._ meta files on unix command line

When a mac accesses storage not formatted in it's own HFS format, it stores it's additional metadata about the file in another file named the same but starting with "._" It can be annoying and when you're accessing a remote volume that's source control managed with subversion, they show up your status information. This bash shell script will get remove of all ._metadata files residing below the current working directory.

#!/bin/sh
for i in `find . -regex '.*\._.*'`; do rm $i; echo "removing $i"; done


If you save that as "/home/$USER/bin/cleanup" and do a "chmod 777 /home/$USER/bin/cleanup" it will be available next time you open a terminal by just typing "cleanup".

Quickly concatenating PDF files with teTeX utilities

Using utilities from the standard teTeX distribution, it's possible to quickly and easily concatenate multiple pdfs together from the command line.

To concatenate 1.pdf, 2.pdf, 3.pdf into a single file 123.pdf you can use the command

texexec --pdfarrange --result 123.pdf 1.pdf 2.pdf 3.pdf


To concatenate all pdf files in a directory into a single file all.pdf you can use

texexec --pdfarrange --result all.pdf *.pdf

Measure the daily number of E-mail messages in a mailbox

This snippet written in bash with calls to perl from the command line measures the number of E-mail messages sent to a mailbox per calendrical day.

#!/bin/bash
grep -h '^Date:' * |
    perl -pe 's!^Date: !!' |
    perl -pe 's!^\w\w\w, !!' |
    perl -pe 's{\d{2}:\d{2}:\d{2}.*$}{}' |
    perl -pe 's!^\s+!!' |
    perl -pae '$_=sprintf("%.2d-%s-%s\n", @F)' |
    sort | uniq -c | sort -n


I used perl for some places where sed would have been more suitable because I find the sed regexp syntax confusing. :-)

yet another lighttpd restart script

#!/bin/sh

USERNAME=username
LIGHTTPD_CONF=/home/$USERNAME/service/lighttpd.conf
PIDFILE=/home/$USERNAME/service/logs/lighttpd.pid
HTTPD=/home/$USERNAME/lighttpd/sbin/lighttpd

PID=0
if [ -e $PIDFILE ]; then
    PID=`cat $PIDFILE`
    if [ "x" == "x$PID" ]; then
        PID=0
    fi
fi

case "$1" in
    start)
        if [ 0 -ne $PID ]; then
            running=`ps --pid $PID | grep $PID`
            if [ $running ]; then
                echo "lighttpd is already running"
                exit 1
            fi
            rm $PIDFILE
            PID=0
        fi
        $HTTPD -f $LIGHTTPD_CONF
        ;;
    stop)
        if [ 0 -eq $PID ]; then
            echo "lighttpd was not running"
            exit 1
        fi
        kill $PID
        tries=""
        while [ -e $PIDFILE ]; do
            if [ "x$tries" == "x.........." ]; then
                break
            fi
            sleep 2
            tries=".$tries"
        done
        if [ -e $PIDFILE ]; then
            echo "lighttpd did not go gentle into that good night, murdering"
            kill -9 $PID
            rm $PIDFILE
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        if [ 0 -eq $PID ]; then
            echo "lighttpd was not running"
        fi
        kill -HUP $PID
        ;;
    *)
        echo "Usage: "`basename $0`" (start|stop|restart|reload)"
        exit 1
        ;;
esac

Reverse DNS from command line

Quick and easy way to look up a domain name given an IP address.

dig -x 17.254.3.183


List 10 largest files/directories within the current directory

From the textdrive fora

du -sk * | sort -n | tail -10


This will list the top 10 largest files and/or directories below the directory where you run this command.

Restart lighttpd script

I use this script to avoid having to do the killall dance on my local dev box constantly when tweaking my lighttpd.conf

#!/bin/sh
sudo killall -9 lighttpd
sudo killall -9 ruby
sudo /opt/local/sbin/lighttpd -f /opt/local/etc/lighttpd.conf


Make the script executable (chmod u+x), put it in your path, invoke by simply typing the name of the script. This also kills off any ruby processes, which lighttpd will restart when it reboots.

The path to lighttpd and the lighttpd.conf files are based on the installation from DarwinPorts - adjust as needed.

Cron zombie killer

This searches for and destroys the zombie processes that linger (and block I/O for an indefinite amount of time) after cron starts up on a shared server:

for each in `ps jauxww | grep Z | grep -v PID | awk '{print $3}'`; do for every in `ps auxw | grep $each | grep cron | awk '{print $2}'`; do kill -9 $every; done; done

Make a backup of all tables startin with...

This will backup all tables starting with "prefix_" to a gzipped file backup.sql.gz.

echo "SHOW TABLES" | mysql -uUSERNAME -pPASSWORD -D DBNAME | grep ^prefix_ | xargs mysqldump -uUSERNAME -pPASSWORD DBNAME | gzip -c > backup.sql.gz


You have to replace USERNAME, PASSWORD and DBNAME twice.

Note: On textdrive you want to add --skip-opt to mysqldump, otherwise the command will abort with an error because privs for lockings tables are missing (for me at least).

To check which tables were backed up, you can use:
zcat test.sql.gz | grep CREATE


Read http://dev.mysql.com/doc/mysql/en/mysqldump.html to find out which other options to add to mysqldump

SLOC of php source code

This shell snippet counts all lines in all .php files (in and beneath the current path ".") that are non-blank and not comments (yes, it also discards multiline comments).

find . -name '*.php' | xargs cat | sed -re ':top /\/\*.*\*\// { s/\/\*.*\*\///g ; t top }; /\/\*/ { N ; b top }' | awk '$0 !~ /^[\t[:space:]]*($|(\/\/)|(#))/' | wc -l

? Earlier 23 items total Later ?