grep to search the subfolders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep to search the subfolders
# 1  
Old 04-18-2011
grep to search the subfolders

Hi everybody,

I am trying to grep abcds in one folder and in all its subfolders!
Quote:
grep -r abcds ./docs/*
but it does not work!

would you help please?

thanks.
# 2  
Old 04-18-2011
The '-r' is not a valid option for the 'grep' command.

Try running without it.
# 3  
Old 04-18-2011
Quote:
Originally Posted by Shell_Life
The '-r' is not a valid option for the 'grep' command.
Perhaps your grep and the OP's differ. -r is not a standard (posix) grep extension, but it is supported by GNU grep. So it should work on linux and freesbsd.

Even if the grep implementation supports -r, it's probably better practice to get in the habit of using -R instead. GNU grep supports -R as a synomym for -r and there are some grep implementations which do not support -r but do recurse with -R.

Also, a few standard utilities have deprecated -r in favor of -R (in posix, for consistency's sake, such as cp).

Regards,
Alister
# 4  
Old 04-18-2011
Code:
string_search /docs abcds

#!/usr/bin/ksh

if [[ $# -ne 2 ]]
then
  echo usage: $0 starting_point string
  exit 1
fi

if [[ ! -d $1 ]]
then
  echo $0: starting_point must be a directory. eg: / or /home
  exit 1
fi

echo $0: Searching files under $1 on $(date)...
find $1 -type f -print 2>/dev/null | sort -u | while read FNAME
do
  #echo Checking $FNAME...
  if [[ $(grep -c "$2" "$FNAME" 2>/dev/null) -gt 0 ]]
  then
      echo "$2 found in: $FNAME  \c  "
      grep "$2" "$FNAME" 2>/dev/null
  fi
done

echo $0: Ended on $(date).

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

Grep for search.

hi, i got files delimited by |^ character. eg. apple|^ball|^cat|^dog|^egg i need to find if the above file contains | (pipe ) character inside the data.eg. apple|^ball|^cat|^d|og|^egg. above file contains | character in the dog data. how can i search that. (4 Replies)
Discussion started by: krk
4 Replies

4. Shell Programming and Scripting

Grep search for value between dates

Hi I am new to scripting and I have a requirement to grep a value from large numbers of xml files and see if this value exist then I want to check the date and see if it falls between two dates (eg: today and feb 17), then print the name of file. the values in xml file is as follow... (7 Replies)
Discussion started by: mmsiddig
7 Replies

5. Shell Programming and Scripting

Re: grep exact search

Hi, x=GATE ps -ef|grep pmon grid 552 1 0 Aug08 ? 00:00:51 asm_pmon_+ASM1 oracle 4314 1 0 Aug08 ? 00:04:08 ora_pmon_GATE1 oracle 5018 1351 0 10:14 pts/1 00:00:00 grep pmon oracle 7329 1 0 Aug08 ? 00:02:19 ora_pmon_NRID1 ps... (3 Replies)
Discussion started by: rcc50886
3 Replies

6. UNIX for Dummies Questions & Answers

Search current folder and subfolders with grep

Hello, Neither ‘Grep -r' nor ‘grep -R' is working in my environment. (Searching for a text pattern in the files) Any suggestions... Using SunOS 5.9 Thanks, Trinanjan. (1 Reply)
Discussion started by: bhanja_trinanja
1 Replies

7. Shell Programming and Scripting

Search and Replace text in folders and Subfolders

Hi, I need help in writing a script to search a particular text in multiple files present in folders and sub folders and replace it with another string which also has special characters like '&', '|', etc.. I know sed command will be used to replace the text but i'm not sure how to use it for... (5 Replies)
Discussion started by: Asheesh
5 Replies

8. Shell Programming and Scripting

Search in folder and subfolders

How can this be done? I mean, I want to search for all *png *jpg *bmp files in my ~/Pictures/ folder....How can I list them? Thank you geeks :) :b: (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

search using grep

Hi all, I have a folder with many files. I need to get all the filenames with the value "<f id=audio-time>*:*:*</f>" word. * may be any integer. I used find . -type f -exec grep -H '<f id=audio-time>.:.</f>' {} \; command to get. but i am not able to find the correct values. Any... (2 Replies)
Discussion started by: ananthi_ku
2 Replies

10. Shell Programming and Scripting

Search through subfolders and move them into separate folder on the base of file size

Hello guys I am sure that you will help me on this issue as you did earlier::) Scenario : I have a folder named "XYZ". It consist many sub-folders and subfolder contain severals files. there may be abc.dat in each subfolder. Now i want to seperate subfolders on follwing conditions- if abc.dat... (12 Replies)
Discussion started by: infiant
12 Replies
Login or Register to Ask a Question