shell scripting style question

Kevin D. Clark kclark at CetaceanNetworks.com
Thu Dec 5 09:22:24 EST 2002


"Price, Erik" <eprice at ptc.com> writes:

> I think "find" is cool but I am not familiar with some of the
> more advanced arguments

Here are some things that I use find for:


Convert a whole directory tree of text files snarfed from a windoze
machine (with ^M's on the end of every line) over to Unix text file
format:

  find /somedir -type f -exec dos2unix {} {} \;


Also, attached are some functions that I have in my .bashrc:

Regards,

--kevin
-- 
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc



-------------- next part --------------
# Author:  Kevin D. Clark (alumni.unh.edu!kdc)

srcfind () {
  if [ $# -eq 0 ] ; then
    echo srcfind: please enter a directory list
  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
    echo writeablesrcfind: please enter a directory list
  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
    echo buildfind: please enter a directory list
  else
    find "${@}" \(   -name \*.c \
                  -o -name \*.cc \
                  -o -name \*.h \
                  -o -name \*.java \
                  -o -name \*.c++ \
                  -o -name Makefile \
                \) -print
  fi
}

newerbuildfind () {
  if [ $# -lt 2 ] ; then
    echo Usage: newerbuildfind file-with-timestamp directory1 directory2 ...
  else
    FILE=$1
    shift
    find "${@}" -newer $FILE \
                \(   -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 file-with-timestamp 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
}



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

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

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

hfind () {
  if [ $# -eq 0 ] ; then
    echo hfind: please enter a directory list
  else
    find "${@}" \(    -name \*.h \
                \) -print
  fi
}

jfind () {
  if [ $# -eq 0 ] ; then
    echo jfind: please enter a directory list
  else
    find "${@}" \(   -name \*.java \
                \) -print
  fi
}

elfind () {
  if [ $# -eq 0 ] ; then
    echo elfind: please enter a directory list
  else
    find "${@}" \(   -name \*.el \
                \) -print
  fi
}

bakfind () {
  if [ $# -eq 0 ] ; then
    echo bakfind: please enter a directory list
  else
    find "${@}" \(   -name \*.bak \
                \) -print
  fi
}


classfind () {
  if [ $# -eq 0 ] ; then
    echo classfind: please enter a directory list
  else
    find "${@}" \(   -name \*.class \
                \) -print
  fi
}

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

readonlyfind () {
  if [ $# -eq 0 ] ; then
    echo readonlyfind: please enter a directory list
  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 2 ] ; then
    echo newerfind: please enter a directory list
  else
    perl -MFile::Find -e '$f = shift; find(sub{print "$File::Find::name\n" if (-M $f > -M);}, @ARGV);' "${@}"
  fi
}


More information about the gnhlug-discuss mailing list