1. hgrep displays grep results in highlights.
2. rgrep runs grep on all the files in a directory tree.

Example usage:
  $ hgrep main *.c; # Search for main in all C files
  $ rgrep -e ‘[Ff]lour’ -e cornstarch $HOME/recipes; # Search all files in the recipes directory for flour or cornstarch

— hgrep —

#! /bin/sh
#  Synopsis:    hgrep pattern file ...
#  Description: Hilighted grep

smso=`tput smso`
rmso=`tput rmso`
pattern="$1"; shift
grep "$pattern" "$@" | sed -e "s@\(${pattern}\)@${smso}\1${rmso}@"

— rgrep —

#! /bin/sh
# Name:         $Id: rgrep,v 1.1 2009/10/11 20:37:21 hhong Exp $
# Synopsis:     rgrep pattern directory ...
# Synopsis:     rgrep [-e pattern ...] pattern directory ...
# Description:  Run grep on all the files of a directory tree

while getopts e:i opt; do
        case $opt in
          e) pattern="$pattern -e '$OPTARG'";;
          i) ignorecase='-i';;
        esac
done
shift `expr $OPTIND - 1`
if [ -z "$pattern" ]; then
        pattern="'$1'"; shift
fi

for arg; do
        if [ ! -d "$arg" ]; then
                echo "$0: Error '$arg' is not a directory." >&2
                exit 1
        fi
done 

find "$@" -type d -print | while read dir; do
        edir=`ls -A "$dir"`
        if [ -n "$edir" ]; then  # Check for empty directory
                # Use eval in case $pattern or $dir contain whitespace characters
                eval grep $ignorecase $pattern "'$dir'/*" /dev/null;  
        fi
done 
  • Set the command line prompt to the current shell level and directory (directory is abbreviated if very long)
    chdir () 
    { 
        if [ $# -eq 2 ]; then
            'cd' `pwd | sed -e "s@$1@$2@"`;
        else
            'cd' "$@";
        fi && {
             if [ "${PWD#/*/*/}" = "$PWD" ]; then
                PS1='[${SHLVL} $PWD] ';
            else
                PS1='[${SHLVL} ..${PWD#${PWD%/*/*}}] ';
            fi;
            case "$TERM" in 
                xterm)
                    echo -ne "33]0;${PWD}07"
                ;;
            esac
        }
    }
    

    Note: add the following alias and environment variable to your $HOME/.profile script:
    alias cd=’chdir’
    SHLVL=0; # for ksh only, also requires ‘(( SHLVL = $SHLVL + 1 ))’ in the ~/.kshrc startup script

  • Pipe the output of a command in to an editor (vi) instead of using more or less.
    function vip
    {
            tmpfile="$HOME/.tmp/$$.vip"
            trap "rm -f $tmpfile" 0 9 15
            cat > $tmpfile
            vi $tmpfile < /dev/tty > /dev/tty 
    }
    

    Note: Requires the $HOME/.tmp directory.
    Example usage: $ ls -l | vip

  • Display the exit code of a program.
    trap "echo ' ' Retcode=\$?" ERR

    Note: Add to your .bashrc or .kshrc startup script

  • List all sub-directories of a specified directory:
    lld () 
    { 
        if [ $# -eq 0 ]; then
            ls -ld */;
        else
            for d in "$@";
            do
                ls -ld ${d:-.}/*/;
            done;
        fi
    }
    

    Example usage:
      $ lsdirs; # display list of subdirectories in the current working directory
      $ lsdirs /usr/local $HOME/logs

  • Copy a directory tree to a remote system:
    $ { cd source_dir; tar cvf - .; } | ssh remote_host '{ cd dest_dir; tar xvf -; }
  • A simplified logfile rolling scheme. Remove all ‘xyz’ log files, except the last $keep files sorted by last modified date. Add to cron for automation.
    $ ls -1t "$logdir/xyz.*.log" | sed -e "1,${keep}d" | xargs -d'\n' rm