Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

How to do search - linux

Hi..
 How to do a search for a particular string inside a file in linux.The difficulty is that i want search all the subfolders of the current directory from which i am doing the search.
pls help
Tony
0
tonyva
Asked:
tonyva
  • 4
  • 3
  • 2
  • +3
1 Solution
 
tbeardenCommented:
the command you want is grep

to search for the string test in the current directory you would do:

grep -r test .

Don't forget the period at the end.
0
 
tonyvaAuthor Commented:
Thanks ...
i am using "grep test *c" for searching for test in current directory. But how can i search all sub folders also like this from my current directory ?
Hope u can help me in this
Tony
0
 
tbeardenCommented:
that is what the -r is for.  It tells it to search recursivly from where you are.
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
tonyvaAuthor Commented:
it is not working here... giving error... I am trying this in LynxOS.
0
 
tbeardenCommented:
try doing a grep --help and look for a recursive option.
0
 
woregulCommented:
Have you tried the following?

    grep -R test .

If that does not work, try this:

    find . -type f -exec grep test '{}' ';'

or for a file pattern

    find . -type f -name '*c' -exec grep test '{}' ';'

0
 
vreddypatilCommented:
try this
Lynx# grep -i "test" */*

--Vijayapal
0
 
tonyvaAuthor Commented:
hi
 the "find ..... " is not working . Some help is that from grep -i -l "test" */*   But that search only one set below the dirctory structure. It is not going in recursively.
Hope some one can give a better option that can work in Lynx
Tony
 
0
 
diamond1Commented:
try this:

grep -r "string" */*

or

grep -d recurse "string" */*

Check out the man pages for more information on the options available with grep
0
 
garbouaCommented:
to include all the subfolders use this
find ./ -name "nameofFiles" |xargs grep string
find will look under "./" which is your current directory, if you would like to specify another , it is okay too.
-name is the name of files you are looking for and it takes wild cards, you can say -name "*" for all files or -name "ma?ile*.c*" for all files that begin wil "ma" folloed by a single character then "ile" punch of character and the extention is of type that begins with c
xargs will pipe the output and run them through grep the output will be like
firstfile: something soemthing yourstring something

so
find /etc -name "pass*" |xargs grep myname
will produce
passwd: myname:50:23:7(*^&%:  etc etc
0
 
tonyvaAuthor Commented:
Wow.... Yes. This works very well  and this was what i wanted .Thank u for the answer and the explanation along with, which help me to understand it very well.
Thank u garboua
Tony
 

 
0
 
garbouaCommented:
:-), to learn more type "man find"  to learn how to use find in conj with other command such as "rm" "tar" "cp" "mv" and all other commands.  in Linux, Unix, find is your best friend
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now