User Tools

Site Tools


ubuntu:bash_-_scripts:tgrep

Ubuntu - Bash - Scripts - tgrep

tgrep searches for character strings in each file, in a file tree.


Usage

tgrep [-c|-h] string [file ...]

Code

#!/bin/bash
#
# @(#) tgrep v1.0  Search for a string in a tree.
 
OPT=""
 
for ARG in $@
do
  if [ "`echo $ARG|cut -c1`" = "-" ]
  then case $ARG in
    -c)  OPT="-name \"*.c\""
         shift;;
    -h)  OPT="-name \"*.h\""
         shift;;
    *)   echo "$O: incorrect argument"             >&2
         echo "usage: $O [-c|-h] string [file ...] >&2
         exit 1;;
  esac
  fi
done
 
case $# in
  0)  echo "$O: argument error"                 >&2
      echo "usage: $O [-c|-h] string [dir ...]" >&2
      exit 2
      ;;
  1)  while read FILE
      do
        grep -y "$1" $FILE /dev/nul
      done
      ;;
  *)  STRING=$1; shift
      eval find "$@" -type f $OPT -print | sort | while read FILE
      do
        grep -y "$STRING" $FILE /dev/null
      done
      ;;
esac

Example Usage

tgrep "profanity" /
tgrep unix $HOME
tgrep -c "^sleep()$" $HOME/src
find /usr/src -name "*.c" -print | tgrep "ioctl"
tgrep "| more" `find . -type f -print`
tgrep trap /bin /usr/bin /etc
grep "$1" `find "$2" -print`

and

find "$2" -print | while read F
do
  grep "$1" $F
done
ubuntu/bash_-_scripts/tgrep.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki