Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total

Change file extensions with bash shell

To change all .htm files in a folder to .html files:

for f in *.htm; do mv $f `basename $f .htm`.html; done;

bash shell directory shortcuts

Go back to your previous directory:

cd -


Go to a new directory and push the current one onto a stack:

pushd /newdirectory


Pop a directory from the stack and go to it:

popd

Resuming an aborted scp transfer

Assume you have copied a file across the network using something like this:

scp host:/path/to/file .


Then assume something goes wrong, so the transfer is interrupted. To continue the transfer, do this:

rsync --partial host:/path/to/file .


(Or, rather, always use rsync as above when transferring very large files.)

(Also note: I don't know how frequently rsync flushes its buffer, but don't assume something is wrong just because the file you're downloading doesn't increase in size in the file system. When you interrupt rsync using Ctrl-C, it will flush its buffers.)

Doing something equivalent of "svn status" in CVS

The "cvs status" command is practically useless, given the amount of stuff it spews out. So I do the following to find out the status of my local working copy. This shows all updated, added, or deleted files, as well as those CVS know nothing about (and which perhaps should be added):

cvs status 2>&1 | egrep "(^\? |Status: )" | grep -v Up-to-date

iTunes arrows search local library

A neat trick in iTunes 4.5 that changes the little arrows next to each artist/album/title to search your library for that string instead of the iTunes Music Store.

defaults write com.apple.iTunes invertStoreLinks -bool YES

turn off disk image verification

defaults write com.apple.frameworks.diskimages skip-verify true

automatically authorize a remote host (SSH key swap)

use with caution! This script will copy your pub key to a remote host so you can login or run remote scripts without a password. IMPORTANT: if you need to run a single script on the remote host, it would be better to authorize a key for just that script.

#!/bin/bash
# kumar.mcmillan -at- farmdev.com

function usage()
{
    echo ""
    echo "Authorizes a host for automatic SSH use by sending your key to the remote host ..."
    echo "Usage: $0 remote_host_to_authorize [username:=defaults to current username]"
    echo ""
}

function cleanup()
{
    if [ -f $TEMP_PUB_KEY_XFER ]
    then
        rm $TEMP_PUB_KEY_XFER
    fi
}

function exit_on_error()
{
    cleanup
    exit 1
}

if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]
then
    usage
    exit 0
fi

PUB_KEY=~/.ssh/id_dsa.pub
if [ $# -eq 2 ]; then
    USER=$2
else
    USER=`whoami`
fi
HOST_TO_AUTH=$1
TEMP_PUB_KEY_XFER=/tmp/$USER"_TEMP_KEY"

echo "checking for $PUB_KEY ..."
if [ ! -f $PUB_KEY ]; then
    echo "generating your dsa public key (leave passphrase blank and save to $PUB_KEY when prompted) ..."
    ssh-keygen -t dsa
    if [ $? -ne 0 ]; then
        echo "ssh-keygen failed"
        exit_on_error
    fi
fi
echo "OK"

echo "for the following commands you will be asked to supply your password for $HOST_TO_AUTH :"

echo "copying a temp pub key to $HOST_TO_AUTH ..."
cat $PUB_KEY > $TEMP_PUB_KEY_XFER
chmod 700 $TEMP_PUB_KEY_XFER
echo "OK"

remote_key=`basename $TEMP_PUB_KEY_XFER`
scp $TEMP_PUB_KEY_XFER $USER@$HOST_TO_AUTH:~/$remote_key
if [ $? -ne 0 ]; then
    echo "scp failed"
    exit_on_error
fi
    
echo "authorizing $HOST_TO_AUTH for automatic SSH use ..."
ssh $USER@$HOST_TO_AUTH "cat ~/$remote_key >> ~/.ssh/authorized_keys; rm ~/$remote_key"
if [ $? -ne 0 ]; then
    echo "ssh failed"
    exit_on_error
fi
echo "OK"

cleanup
echo "authorization successful!  you can now login automatically to $HOST_TO_AUTH"
exit 0

Browse pydoc locally ...

Opens pydoc in your browser. If pydoc has not been started it starts it for you.

#!/bin/bash

PYDOC=`which pydoc`
if [ ! -x ${PYDOC} ]
then
    echo "could not find executable pydoc (tried: ${PYDOC})"
    exit 1
fi

# passthru ...
if [ $# -gt 0 ]
then
    ${PYDOC} $@
    exit $?
fi

#--------------------------------------
# or else... start it up automagically :

PYDOC_PORT=9000
PYDOC_SERVER=http://localhost:${PYDOC_PORT}/
running=0
pydoc_pses=0

function browse_docs()
{
    open ${PYDOC_SERVER}
}

for ps in `ps ax | grep "${PYDOC}" | awk '{ print $1 }'`
do
    let pydoc_pses++
done

if [ $pydoc_pses -ge 2 ]
then
    browse_docs
else
    ${PYDOC} -p ${PYDOC_PORT} &
    until `curl -o /dev/null ${PYDOC_SERVER} &>/dev/null`
    do
        echo "waiting for pydoc server ..."
        sleep 2
    done
    browse_docs
fi
exit 0

Get a screenshot

screencapture -S /tmp/screengrab.pdf ; sips -s format png /tmp/screengrab.pdf --out /tmp/screengrab.png; open /tmp/screengrab.png
« Newer Snippets
Older Snippets »
Showing 11-19 of 19 total