Solved

How to do search - linux

Posted on 2001-06-20
12
451 Views
Last Modified: 2010-04-20
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
Comment
Question by:tonyva
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 4

Expert Comment

by:tbearden
ID: 6212631
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
 

Author Comment

by:tonyva
ID: 6212658
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
 
LVL 4

Expert Comment

by:tbearden
ID: 6212784
that is what the -r is for.  It tells it to search recursivly from where you are.
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.

 

Author Comment

by:tonyva
ID: 6212878
it is not working here... giving error... I am trying this in LynxOS.
0
 
LVL 4

Expert Comment

by:tbearden
ID: 6216497
try doing a grep --help and look for a recursive option.
0
 

Expert Comment

by:woregul
ID: 6220025
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
 
LVL 1

Expert Comment

by:vreddypatil
ID: 6232363
try this
Lynx# grep -i "test" */*

--Vijayapal
0
 

Author Comment

by:tonyva
ID: 6233449
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
 

Expert Comment

by:diamond1
ID: 6239524
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
 
LVL 5

Accepted Solution

by:
garboua earned 100 total points
ID: 6244520
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
 

Author Comment

by:tonyva
ID: 6247092
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
 
LVL 5

Expert Comment

by:garboua
ID: 6249188
:-), 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
awk file 6 77
Run Secure WMI query from CentOS 5 79
Moving from Mcrypt to OpenSSL 18 45
expand ext4 on centos 6 5 37
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

789 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