Solved

fgrep subfolders - files

Posted on 2006-11-08
9
636 Views
Last Modified: 2007-12-19
Hi

I have a situation were I need to display the file names that contain the searching word.
I think we can search for word in file using fgrep. But when it come to subfolders or other file how can we do that.
lets say we have root -> subfolder 1 --> file (contain text then diplay file name).
insted of search in one single file how can we do it in all file and subfolder.
It would be great if you have any sample program that has same functionality.

Thanks
0
Comment
Question by:basirana
9 Comments
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 100 total points
ID: 17899582
If you are searching for whatever, use

grep -ir "whatever" root
0
 
LVL 15

Assisted Solution

by:DonConsolio
DonConsolio earned 100 total points
ID: 17902485
fgrep --files-with-matches --recursive --no-messages 'PATTERN' /path/to/file_or_directoy
0
 
LVL 10

Assisted Solution

by:ssvl
ssvl earned 100 total points
ID: 17905061
why not

find /path/to/directory -name *PATTERN*

i believe that you are searching only for FILENAMES here and not in content.  grep searches through the files and gives you a list of maatches
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 100 total points
ID: 17906135
Well, what one should really do is

find /path/to/directory -name '*searchword*'

or

find /path/to/directory -name \*searchword\*

To search for files that have 'searchword' in the filename.

The pattern needs to either be quoted, or you need to escape special characters that the shell is not to interpret.


If the shell you type the 'find' command in does globbing, and you don't quote the pattern, the shell,
can replace  *PATTERN* with the list of any files in the current directory (if any) that happen
to match the pattern, before find even starts.

0
 
LVL 3

Accepted Solution

by:
bryanlloydharris earned 100 total points
ID: 17909204
cd /some/path
grep -R "search-pattern" .

# there is a dot at the end of line 2
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 17909615
Hi,

-R switch is to recurse into directories below the current folder
-l switch will display filenames instead of matching line contents

so:

fgrep -R -l  <pattern> *


will match the files with the pattern recursively and will displayy only the names of the files.

Cheers,
K.
0
 
LVL 11

Expert Comment

by:kblack05
ID: 17911067
Find works, but grep with the -R switch is much less system intensive, since find is likely to tap system resources if its a big file structure...

Also check out "egrep"

The -i (ignore case) and -R (recurse substructure) are our friends as KeremE points out.

man find
man grep
man egrep
0
 
LVL 58

Expert Comment

by:amit_g
ID: 17933069
basirana, could you please explain why you have chosen that one as answer? If that works for you, the comment posted a day earlier http:#17899582 should also work.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
App holding yum lock unable to update my rpm package 1 55
How to learn Linux? 10 61
Run same command on multiple files in Linux 3 33
awk sed 8 45
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question