How can i find a word inside a file from a directory and it's subdirectory ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i find a word inside a file from a directory and it's subdirectory ?
# 1  
Old 10-09-2011
MySQL How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail".
I have to search this word in all files inside a directory and it's sub-directories.

It will also search in all hidden directory and sub-directories.

If it finds this word in any file it will list that file.

How can i do this with perl/ruby/awk/sed/bash or using any command ?
# 2  
Old 10-09-2011
Code:
find /directory -type f -exec grep -l mail {} \;

# 3  
Old 10-09-2011
Or the more efficient:
Code:
find /directory -type f -exec grep -l mail {} +

# 4  
Old 10-09-2011
@jlliagre , @jim mcnamara,

Is it possible to get the same result with only grep command ?

---------- Post updated at 07:11 PM ---------- Previous update was at 07:05 PM ----------

Quote:
Originally Posted by jim mcnamara
Code:
find /directory -type f -exec grep -l mail {} \;

I tried that , but it did not print any filename .

But if i do
Code:
grep -lri "mail" .

It prints some filename.

Actually what i'm trying to do is to print those filename which file content the word "mail". That means if i do
Code:
cat filename | grep mail

I will get some lines.
# 5  
Old 10-09-2011
Here's what you want:
Code:
find ./ -type f -print0 | xargs -0 grep -n "mail"

This code shows the file name and the line number where "mail" is found.
Output:
Code:
/home/unixuser/.gnupg/gpg.conf:102:# Example email keyserver:
/home/unixuser/.gnupg/gpg.conf:103:#      mailto:pgp-public-keys@keys.pgp.net
/home/unixuser/.gnupg/gpg.conf:129:#keyserver mailto:pgp-public-keys@keys.nl.pgp.net
/home/unixuser/.gnupg/gpg.conf:201:# photo-viewer "metamail -q -d -b -c %T -s 'KeyID 0x%k' -f GnuPG"

and if you want to match with just the word "mail", use this:
Code:
find ./ -type f -print0 | xargs -0 grep -n " mail "

Note the spaces before and after mail within quotes. For case-insensitive search, add -i with grep:
Code:
find ./ -type f -print0 | xargs -0 grep -in " mail "


Last edited by admin_xor; 10-09-2011 at 02:31 PM..
# 6  
Old 10-09-2011
Check if your grep supports -r option, if so you can do it this way
Code:
grep -rli mail *

--ahamed
# 7  
Old 10-09-2011
Quote:
Originally Posted by cola
I tried that , but it did not print any filename .
Hmm, that doesn't make sense. Try:
Code:
find . -type f -exec grep mail {} +

This does pretty much the same thing, just a simpler and cleaner way, as the convoluted alternative:
Code:
find ./ -type f -print0 | xargs -0 grep ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching File in Directory and all Subdirectory and Delete

Hi All, My directory structure is like Directory1 SubDirectory1 SubDirectory2 SubDirectory3 I have main directories and subdirectories underneath. I want to write a shell script where I will be passing file name as a parameter, Now I want to find all the files in Directory1... (19 Replies)
Discussion started by: John William
19 Replies

2. Shell Programming and Scripting

Move all files not in a directory into a subdirectory named for each given file

Hi Everyone! Looking for some help with a script that will take all files in any given root folder (which are not already in a folder) and put them into separate folders with the name of each given file. Any ideas? Thank you! (1 Reply)
Discussion started by: DanTheMan
1 Replies

3. UNIX for Dummies Questions & Answers

command to find cksum of all files inside a directory?

I did this: ls -lrRt | grep ^* | cksum * but it is showing cksum of sub-directories. Thanks You Please use code tags when posting data and code samples, thank you. (3 Replies)
Discussion started by: ezee
3 Replies

4. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

5. Shell Programming and Scripting

Sum of file size in directory / subdirectory

Hi , I am trying to write something to find the size of particular type of files in a directory & it's subdirectory and sum the size .. These types of file are found at directory level or its subdirectories level .. #!/bin/ksh FNAME='.pdf' S_PATH=/abc/def/xyz find $S_PATH -exec ls -lad... (4 Replies)
Discussion started by: Vaddadi
4 Replies

6. Shell Programming and Scripting

Find files inside the parent directory only

Hi All, The following find command lists the files which are 45 minutes older. But it searches for the sub directories also. $ find . -type f -mmin +45 -print ./hello.txt ./test/hi.txt ./temp/now.txt ls hello.txt test temp How can i modify the find command in such way that it finds... (4 Replies)
Discussion started by: Tuxidow
4 Replies

7. Shell Programming and Scripting

Find command, -name by directory and subdirectory?

Hi All, I'm trying to use the find command to return matches for a directory and file. For example, given the following directories: /one/two/three/file1.txt /one/three/two/file1.txt /one/four/two/three/file1.txt I'm expecting the following to be returned: ... (16 Replies)
Discussion started by: makodarear
16 Replies

8. Shell Programming and Scripting

Find files in directory and its subdirectory

I am writing a script which reads a file line by line and then assigns it to a variable like this 1090373422_4028715212.jpg. I have images with file name of this format in some other directory. In my script I want to assign variable with this file name and then find this filename in some other... (11 Replies)
Discussion started by: jyotib
11 Replies

9. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies

10. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies
Login or Register to Ask a Question

Featured Tech Videos