The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove directory with subdirectories and files? ppa108 UNIX for Dummies Questions & Answers 9 11-25-2008 02:02 AM
searching content of files in the current and sub directories tiger99 Shell Programming and Scripting 4 01-23-2008 03:11 AM
list largest files in a directory & its subdirectories igidttam UNIX for Dummies Questions & Answers 6 09-25-2006 11:31 AM
Searching for files over 30 days old in current directory cxredd4 Shell Programming and Scripting 18 06-11-2006 02:16 AM
Searching files within subdirectories jalvarez UNIX for Dummies Questions & Answers 1 09-08-2003 04:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-15-2007
milagros milagros is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
Question searching files through all subdirectories beneath the current directory

i want to make a bash script that searches a specific pattern in files through all subdirectories beneath the current directory..without using the command grep-R
but only the command grep..

e.g
for i in *
do
grep "pattern" $i
.....
...
done

using the character (*) the script searches only in files beneath the current directory...how can i make it?????
any help would be appreciated
  #2 (permalink)  
Old 05-15-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Milagros,
See if this would work for you:
Code:
find . -type f -exec grep 'pattern' {} \;
  #3 (permalink)  
Old 05-15-2007
milagros milagros is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
thanks for your reply..but i asked something else...
my code is:
if [ $# -eq 1 ]
then
echo "give the parameter"
exit 1
fi

for i in *
do
grep "$1" $i
if [ $? -eq 0 ]
then
echo "File $i"
grep $1 $i
fi
done


when i run this script...the message is segmentation fault..
what is the problem...???
  #4 (permalink)  
Old 05-15-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Milagro,
Here is your request again:
Quote:
i want to make a bash script that searches a specific pattern in files through all subdirectories beneath the current directory..without using the command grep-R
but only the command grep..
The solution I gave with the "find" command satisfy what you asked for.
Did you test the solution?
What you are doing now is changing your own original specs.
  #5 (permalink)  
Old 05-15-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Milagros,
I hope this is not a homework -- it smells like it.
In any event, here is your shell corrected:
Code:
if [ $# -ne 1 ]
then
  echo "give the parameter"
  exit 1
fi

for i in *
do
  mTot=`grep -c ${1} $i`
  if [ ${mTot} -gt 0 ]
  then
    echo "File $i"
  fi
done
  #6 (permalink)  
Old 05-15-2007
milagros milagros is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
thanks for your reply...it's only a book exercise and it's just for practice.it's not a homework... i am pretty new shell scripting ...
thanks again
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0