searching files through all subdirectories beneath the current directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching files through all subdirectories beneath the current directory
# 1  
Old 05-15-2007
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 Smilie
# 2  
Old 05-15-2007
Milagros,
See if this would work for you:
Code:
find . -type f -exec grep 'pattern' {} \;

# 3  
Old 05-15-2007
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  
Old 05-15-2007
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  
Old 05-15-2007
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  
Old 05-15-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Searching for a files based on current date directory

Hi All, I've been trying to do some recursive searching but not been very successful. Can someone please help. Scenario: I have directory structure /dir1/dir2/dir3/ 2019/ 11/ 17 18 19 20 so what I want to do is run a script and as its 2019/11/18/ today it would go and only search... (3 Replies)
Discussion started by: israr75
3 Replies

2. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

Find/searching files in subdirectories excluding the fiels in Parent Directory

Hi All, requirement is to find and remove the files from sub directories but it should exclude the files from parent directory. At present i am using the below one but it finds and remove files from both parent and sub directories. find ${PATH} -type f \( -name securitas\* -o -name \*gz... (1 Reply)
Discussion started by: Naveenkk
1 Replies

4. Shell Programming and Scripting

Find files only in current directory...not subdirectories

Hi, I have to find files only in the current directory...not in the sub directories. But when I use Find command ... it searches all the files in the current directory as well as in the subdirectories. I am using AIX-UNIX machine.Please help..I tried to use maxdepth..but it is not working in AIX. (2 Replies)
Discussion started by: vsachan
2 Replies

5. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies

6. UNIX for Dummies Questions & Answers

How to remove directory with subdirectories and files?

I'm trying to remove several directories which contains sun-dirs and files inside. I used the command rm -r <dirname> But, it always ask "examine file in directory <dirname> yes/no?" line by line. So, i need to write "y" for every line. How can i skip this step and remove all directories with... (9 Replies)
Discussion started by: ppa108
9 Replies

7. Shell Programming and Scripting

search files in a directory and its subdirectories

Hello my friends, I need to write a simple shell bad file :D that search and delete a file it's name 'Microsoft.txt' in the current directory and its subdirectories? So can you help to guide me how i can write this shell, Just give me the beginning :o thank you. (1 Reply)
Discussion started by: Net-Man
1 Replies

8. Shell Programming and Scripting

searching content of files in the current and sub directories

Hi I was wondering why command 2 doesn't work like command 1 below. 1. find . -exec grep "test" '{}' \; -print 2. ls -R | grep "test" I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2? Thanks in advance for your help (4 Replies)
Discussion started by: tiger99
4 Replies

9. Shell Programming and Scripting

Searching for files over 30 days old in current directory

May be a simple question for experts here.... I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories. Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Discussion started by: cxredd4
18 Replies

10. UNIX for Dummies Questions & Answers

Searching files within subdirectories

I need to concatenate files that are inside a directory and subdirectories. Those files end with .c Can anyone help me I am using comand 9find) and (cat) but they don't work together.:confused: (1 Reply)
Discussion started by: jalvarez
1 Replies
Login or Register to Ask a Question