LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2009, 06:15 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Rep: Reputation: 49
how to search word in group of files using command


hi
i am using openSUSE 10.3 & like to use power of terminal to do tasks.I want to search all files containing word 'echo' in given folder.What should be command.
 
Old 10-10-2009, 06:22 AM   #2
Ahmed
Member
 
Registered: May 2005
Location: München, Germany
Distribution: Slackware, Arch
Posts: 386

Rep: Reputation: 41
Try the "grep" command. Go into the directory you mean via command line and do (//EDIT: example improved to avoid confusion):

Code:
$ grep "yoursearchpattern" *
It will go through all files inside the folder (therefore the '*') and tell you which ones contain the search pattern. For more options execute 'man grep' to read the instructions..

The grep command is very useful by the way if you want to view the output of a file and filter this output for a given word. Say you have a file called blah.conf and want to view its contents:

Code:
$ cat blah.conf
To find out whether or not there's a line containing the word 'example' without having to read through the entire file, you pipe it's output to the grep command using the symbol |, which returns only the lines with that word, as follows:

Code:
$ cat blah.conf |grep example
Try it out with a file and search term of your choice. Just thought I'd give you an example of the power of the command line

-A

Last edited by Ahmed; 10-10-2009 at 08:18 PM.
 
Old 10-10-2009, 06:26 AM   #3
mariogarcia
Member
 
Registered: Sep 2005
Distribution: debian, solaris 10
Posts: 197

Rep: Reputation: 31
you could do

find . | xargs grep -i pattern

it will show all the files in the current folder that have the pattern whether it is in lowercase or uppercase.

kind regards
 
Old 10-10-2009, 06:46 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Ahmed View Post
Try the "grep" command. Go into the directory you mean via command line and do:

Code:
$ grep (yoursearchpattern) *
[/CODE]
Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:
$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'
 
Old 10-10-2009, 07:14 PM   #5
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7
Posts: 874

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by lutusp View Post
Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:
$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'
Wouldn't it be like

Code:
$ grep /(yoursearchpattern/) *
 
Old 10-10-2009, 07:30 PM   #6
Doculus
LQ Newbie
 
Registered: Oct 2009
Distribution: Gentoo
Posts: 29

Rep: Reputation: 17
Quote:
Originally Posted by mariogarcia View Post
find . | xargs grep -i pattern
Like this you will have a process started for each of the file, plus 2 for find and xargs. Not very environment friendly. Also you loose information in which file the pattern was found, which can be interesting also, or maybe the only interest?

Code:
grep -i pattern -r .
Results in 1 process start, and you will get the filenames also. If you are not interested in the whole result lines, only filenames, you can even write:

Code:
grep -i pattern -rc .
See manpage: grep(1)
 
Old 10-10-2009, 07:36 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by jmc1987 View Post
Wouldn't it be like

Code:
$ grep /(yoursearchpattern/) *
No, you would use this:

Code:
$ grep \(yoursearchpattern\) *
But this is more reliable and less confusing:

Code:
$ grep "(yoursearchpattern)" *
 
Old 10-10-2009, 08:12 PM   #8
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Rep: Reputation: 35
Quote:
grep -ir <pattern_name> *
job done!
 
Old 10-10-2009, 08:17 PM   #9
Ahmed
Member
 
Registered: May 2005
Location: München, Germany
Distribution: Slackware, Arch
Posts: 386

Rep: Reputation: 41
Quote:
Originally Posted by lutusp View Post
Not a very good example. The parentheses need to be quoted to keep Bash from mistaking your intentions:


Code:
$ grep (yoursearchpattern) *
bash: syntax error near unexpected token `yoursearchpattern'
I didn't mean the parenthesis to be part of the search pattern, in lots of cases they don't. It was just a way of highlighting where the search pattern should go. Anyway to make things clearer, I edited my first post. Thanks for the tip.

-A
 
Old 10-10-2009, 08:18 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,330

Rep: Reputation: 227Reputation: 227Reputation: 227
Code:
ls | grep "pattern"
is that it?
 
Old 10-10-2009, 08:34 PM   #11
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Quote:
Originally Posted by smeezekitty View Post
Code:
ls | grep "pattern"
is that it?
It will only search file names, not their contents.
 
Old 10-10-2009, 09:07 PM   #12
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Ahmed View Post
I didn't mean the parenthesis to be part of the search pattern, in lots of cases they don't. It was just a way of highlighting where the search pattern should go. Anyway to make things clearer, I edited my first post. Thanks for the tip.

-A
I assumed you included the parentheses for a reason -- to suggest a regular expression. Parentheses are often used in grep searches, like this example:

Code:
find -type f | grep -iP "\.(gif|jpe?g|png)$" | while read path
do
  ...
done
This example selects files that end in common graphic file suffixes. When I see parentheses in grep examples, I assume they are meant to represent a regular-expression multiple-choice selector like this example.

In any case, the parentheses need to be managed to keep them from being interpreted by the shell -- that was my earlier point.
 
Old 10-10-2009, 10:52 PM   #13
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
Quote:
Originally Posted by sumeet inani View Post
hi
i am using openSUSE 10.3 & like to use power of terminal to do tasks.I want to search all files containing word 'echo' in given folder.What should be command.
Code:
ls -l | grep <pattern>
 
Old 10-12-2009, 01:15 AM   #14
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
WHAT I HAVE LEARNT
I noticed that if I write
grep pattern *
then it gives file with corresponding lines.Also I should enclose search parameter in single or double quotes if it contains space.
 
Old 10-12-2009, 01:30 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You should also use quotes when the search pattern has any wildcards---and in a few other situations.

You should also read up on the difference between single and double quotes---I would start with the Bash Guide for Beginners---free at http://tldp.org
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
Web Development --- Command to search string through files on a server mobby1982 Programming 2 04-11-2009 07:14 PM
Need to search all files for string - strings command? jmur Programming 4 03-28-2008 03:35 PM
Word search Alien_Hominid Slackware 5 04-19-2006 06:52 AM
I have a harddrive that I want to search for deleted word files, what do I do first? abefroman Linux - Security 2 07-31-2005 08:37 PM


All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration