Solved

fgrep subfolders - files

Posted on 2006-11-08
9
628 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 14

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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now