bash find aliases

Kevin D. Clark clark_k at pannaway.com
Fri Dec 17 15:14:01 EST 2004


These are some bash functions that I have written over the years.  In
particular, I find srcfind and txtfind to be very handy in my work.  I
hope that you do as well.

(for example:   srcfind /usr/src | xargs grep SOME_CONSTANT
                txtfind /etc | xargs grep -i dhcp
)

I've distributed these to this list before, but recently I hacked
together "dostxtfind", which I had a need for.

Enjoy!

--kevin




# Author: kevin d. clark (alumni.unh.edu!kdc)

srcfind () {
  if [ $# -eq 0 ] ; then
    srcfind .
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.hh \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name \*.el \
                \) -print
  fi
}

writeablesrcfind () {
  if [ $# -eq 0 ] ; then
    writeablesrcfind .
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.hh \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name \*.el \
                \) \
                -exec test -w {} \; \
                -print
  fi
}


# files that are relevant to our build
buildfind () {
  if [ $# -eq 0 ] ; then
    buildfind .
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name Makefile \
                \) -print
  fi
}

# files that are relevant to our build
newerbuildfind () {
  if [ $# -lt 1 ] ; then
    echo Usage: newerbuildfind file-with-timestamp directory1 directory2 ...
  elif [ $# -lt 2 ] ; then
    file_with_timestamp=$1 ; shift
    newerbuildfind "$file_with_timestamp" .
  else
    file_with_timestamp=$1 ; shift
    find "${@}" -newer $file_with_timestamp \
                \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name Makefile \
                \) -print
  fi
}

# files that are relevant to our build
writablebuildfind () {
  if [ $# -eq 0 ] ; then
    echo Usage: writablebuildfind directory1 directory2 ...
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name Makefile \
                \) \
		-exec test -w {} \; \
           -print
  fi
}


# Finds text files in the specified directories.  These use Perl's -T and -B
# tests.  Here's some relevant documentation from the perlfunc page:
#
#    The "-T" and "-B" switches work as follows.  The first block or
#    so of the file is examined for odd characters such as strange
#    control codes or characters with the high bit set.  If too many
#    strange characters (>30%) are found, it's a "-B" file, other-
#    wise it's a "-T" file.  Also, any file containing null in the
#    first block is considered a binary file. [....]  Both "-T" and
#    "-B" return true on a null file...
#
# Caveat programmer.
# 

# Find text files
txtfind () {
  if [ $# -eq 0 ] ; then
    txtfind .
  else
    perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f && -T);}, @ARGV);' "${@}"
  fi
}

# Find DOS-formatted text files
dostxtfind () {
  if [ $# -eq 0 ] ; then
    dostxtfind .
  else
    perl -MFile::Find -e 'find(sub{ 
                                     $crlf = 0;
                                     if (($f = -f) && ($T = -T)) {
                                       @ARGV=($_);
                                       binmode(ARGV);
                                       (/\r\n/ && $crlf++) while(<>);
                                     }
                                     print "$File::Find::name\n" 
                                       if ($f && $T && $crlf);
                                   }, @ARGV)' "${@}"
  fi
}

# Find binary files
binfind () {
  if [ $# -eq 0 ] ; then
    binfind .
  else
    perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f && -B);}, @ARGV);' "${@}"
  fi
}

cfind () {
  if [ $# -eq 0 ] ; then
    cfind .
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.c++ \
                \) -print
  fi
}

hfind () {
  if [ $# -eq 0 ] ; then
    hfind .
  else
    find "${@}" \(    -name \*.h \
                \) -print
  fi
}

jfind () {
  if [ $# -eq 0 ] ; then
    jfind .
  else
    find "${@}" \(   -name \*.java \
                \) -print
  fi
}

elfind () {
  if [ $# -eq 0 ] ; then
    elfind .
  else
    find "${@}" \(   -name \*.el \
                \) -print
  fi
}

bakfind () {
  if [ $# -eq 0 ] ; then
    bakfind .
  else
    find "${@}" \(   -name \*.bak \
                \) -print
  fi
}


classfind () {
  if [ $# -eq 0 ] ; then
    classfind .
  else
    find "${@}" \(   -name \*.class \
                \) -print
  fi
}

xmlfind () {
  if [ $# -eq 0 ] ; then
    xmlfind .
  else
    find "${@}" \(   -name \*.xml \
                \) -print
  fi
}

writeablefind () {
  if [ $# -eq 0 ] ; then
    writeablefind .
  else
    perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f && -w);}, @ARGV);' "${@}"
  fi
}

readonlyfind () {
  if [ $# -eq 0 ] ; then
    readonlyfind .
  else
    perl -MFile::Find -e 'find(sub{print "$File::Find::name\n" if (-f && ! -w);}, @ARGV);' "${@}"
  fi
}

# Finds all files and directories newer than a given file
newerfind () {
  if [ $# -lt 1 ] ; then
    echo Usage: newerfind file-with-timestamp directory1 directory2 ...
  elif [ $# -lt 2 ] ; then
    file_with_timestamp=$1 ; shift
    newerfind "$file_with_timestamp" .
  else
    file_with_timestamp=$1 ; shift
    perl -MFile::Find -e '$f = shift; find(sub{print "$File::Find::name\n" if (-M $file_with_timestamp > -M);}, @ARGV);' "${@}"
  fi
}





-- 
GnuPG ID: B280F24E                     And the madness of the crowd
alumni.unh.edu!kdc                     Is an epileptic fit
                                       -- Tom Waits



More information about the gnhlug-discuss mailing list