Stop data retention! Click here & act! Are you a webmaster and want to participate? Here you can find all necessary material for your website - Willst du auch an der Aktion teilnehmen? Hier findest du alle relevanten Infos und Materialien:
Chris Karakas Online Forum Index Karakas Online
 FAQFAQ   Forum SearchForum Search   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
How to search recursively the files of a given directory



 
Post new topic   Reply to topic
   Chris Karakas Online Forum Index -> Linux Forum RSS Feed of this Forum
Share this page: These icons link to social bookmarking sites where readers can share and discover new web pages.Digg  del.icio.us  tc.eserver.org  Blinklist  Furl  Reddit  Blogmarks  Magnolia  Sphere  Yahoo!  Google  Windows Live  Technorati  Blue Dot  Simpy  Newsvine  Stumble Upon  co.mments.com  Blinkbits  BlogMemes  Connotea  View previous topic :: View next topic  
Author Message
chris
Dark Lord of the Sith


Joined: 10 May 2003
Posts: 6257
Location: Outer Space

PostPosted: Tue Apr 20, 2004 9:01 pm    Post subject: How to search recursively the files of a given directory
Reply with quote

Question Problem: Find my_search_pattern in *all* files of /directory/to/search and *all* directories beneath it. Take special care for directories with blanks in their names. Your solution must work even if there are thousands of files to check. This means that solutions that are in principle correct, but give errors of type:

Code:

Argument list too long


will NOT be accepted!

Arrow Solution:

Code:

/usr/bin/find /directory/to/search -type f -print0 | /usr/bin/xargs -0 /usr/bin/grep my_search_pattern


Some remarks are in order:

The pipe to xargs will take care of an excessive number of files automatically - if there are too many of them, it will process them through grep and then will continue to process the rest!

The -print0 option tells find to append a 0 after each filename, the -0 option to xargs tells it to delimit filenames by 0 and not by blank. Thus we are able to process files with blanks in their names.

I was tired of having to type this each and every time I need it, so I wrote a script. I call it grepdir, it has the same syntax as grep and comes with a rudimentary help function which outputs the syntax and an example if you call it without arguments. I use grepdir quite often. Here it is:

Code:

#! /bin/sh
#
# /usr/local/bin/grepdir
#
# Author: Chris Karakas
# http://www.karakas-online.de
#
# Searches the files of  directory DIR recursively for
# the expression EXP, excluding the contents of EXCLUDE_DIR
# from the search, if any.
#
# Usage: grepdir EXP DIR [EXCLUDE_DIR]

# Help function
#
function help() {
cat <<-EOF
Usage: grepdir EXP DIR [EXCLUDE_DIR]

Searches the files of  directory DIR recursively for
the expression EXP, excluding the contents of EXCLUDE_DIR
from the search, if any.

  -h, --help    Display this help text
EOF
}

# Check arguments and issue a help statement, if wrong
#
if [ $# -eq 0 ]; then
  help
  exit 1
elif [ $1 = "-h" -o $1 = "--help" ]; then
  help
  exit 0
elif [ $# -eq 1 -o $# -gt 3 ]; then
  help
  exit 1
fi



FIND="/usr/bin/find"
XARGS="/usr/bin/xargs"
GREP="/usr/bin/grep"

# Program name
PN=${0##*/}

arg1=$1
shift

if [[ X"$2" = X"" ]]; then
  echo "$PN: running $FIND $1 -type f -print0 | $XARGS -0 $GREP $arg1"
  $FIND $1 -type f -print0 | $XARGS -0 $GREP $arg1
else
  echo "$PN: running $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP $arg1"
  $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP $arg1
fi


Just type "grepdir" or "grepdir -h" to get a help message.
_________________
Regards

Chris Karakas
www.karakas-online.de


Last edited by chris on Thu Apr 22, 2004 12:26 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
chris
Dark Lord of the Sith


Joined: 10 May 2003
Posts: 6257
Location: Outer Space

PostPosted: Thu Apr 22, 2004 12:05 pm    Post subject:
Reply with quote

There is only a small inadequacy in the above script: you cannot serach for a phrase that contains blanks:

Suppose I want to search for "console with" in all files under /etc. If I try only to escape the blank:

Code:

grepdir console\ with /etc/


I get:

Code:

grepdir: running /usr/bin/find /etc/ -type f -print0 | /usr/bin/xargs -0 /usr/bin/grep console with
grep: with: No such file or directory


If I try

Code:

grepdir "console with" /etc/


I get:

Code:

/usr/local/bin/grepdir: line 32: [: too many arguments
grepdir: running /usr/bin/find /etc/ -type f -print0 | /usr/bin/xargs -0 /usr/bin/grep console with
grep: with: No such file or directory


If I try:

Code:

grepdir "console\ with" /etc/


I get:

Code:

grepdir: running /usr/bin/find /etc/ -type f -print0 | /usr/bin/xargs -0 /usr/bin/grep console\ with
grep: Trailing backslash
grep: Trailing backslash
grep: Trailing backslash
grep: Trailing backslash


Also, sometimes I get garbage that looks like

Code:

;;;2c2c2c2c2c;2c2;


What am I doing wrong?
_________________
Regards

Chris Karakas
www.karakas-online.de
Back to top
View user's profile Send private message Send e-mail Visit poster's website
chris
Dark Lord of the Sith


Joined: 10 May 2003
Posts: 6257
Location: Outer Space

PostPosted: Thu Apr 22, 2004 12:17 pm    Post subject:
Reply with quote

Thanks to Kerry for pointing this to me:

$arg1 must also be enclosed in quotes, then you can use

Code:

grepdir "console with" /etc/


for search for "console with" in all files in and under /etc.

The corrected script source for grepdir is therefore:

Code:

#! /bin/sh
#
# /usr/local/bin/grepdir
#
# Author: Chris Karakas
# http://www.karakas-online.de
#
# Searches the files of  directory DIR recursively for
# the expression EXP, excluding the contents of EXCLUDE_DIR
# from the search, if any.
#
# Usage: grepdir EXP DIR [EXCLUDE_DIR]

# Help function
#
function help() {
cat <<-EOF
Usage: grepdir EXP DIR [EXCLUDE_DIR]

Searches the files of  directory DIR recursively for
the expression EXP, excluding the contents of EXCLUDE_DIR
from the search, if any.

  -h, --help    Display this help text
EOF
}

# Check arguments and issue a help statement, if wrong
#
if [ $# -eq 0 ]; then
  help
  exit 1
elif [ "$1" = "-h" -o "$1" = "--help" ]; then
  help
  exit 0
elif [ $# -eq 1 -o $# -gt 3 ]; then
  help
  exit 1
fi



FIND="/usr/bin/find"
XARGS="/usr/bin/xargs"
GREP="/usr/bin/grep"

# Program name
PN=${0##*/}

arg1=$1
shift

if [[ X"$2" = X"" ]]; then
  echo "$PN: running $FIND $1 -type f -print0 | $XARGS -0 $GREP \"$arg1\""
  $FIND $1 -type f -print0 | $XARGS -0 $GREP "$arg1"
else
  echo "$PN: running $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP \"$arg1\""
  $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP "$arg1"
fi

_________________
Regards

Chris Karakas
www.karakas-online.de
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Share this page: These icons link to social bookmarking sites where readers can share and discover new web pages.Digg  del.icio.us  tc.eserver.org  Blinklist  Furl  Reddit  Blogmarks  Magnolia  Sphere  Yahoo!  Google  Windows Live  Technorati  Blue Dot  Simpy  Newsvine  Stumble Upon  co.mments.com  Blinkbits  BlogMemes  Connotea 
Display posts from previous:   
Post new topic   Reply to topic
   Chris Karakas Online Forum Index -> Linux Forum
Page 1 of 1
This page contains valid HTML 4.01 Transitional - click here to check it!
This page contains a valid CSS - click here to check it!

 

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group