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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
10-10-2009, 06:15 AM
|
#1
|
Member
Registered: Oct 2008
Posts: 908
Rep:
|
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.
|
|
|
10-10-2009, 06:22 AM
|
#2
|
Member
Registered: May 2005
Location: München, Germany
Distribution: Slackware, Arch
Posts: 386
Rep:
|
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:
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.
|
|
|
10-10-2009, 06:26 AM
|
#3
|
Member
Registered: Sep 2005
Distribution: debian, solaris 10
Posts: 202
Rep:
|
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
|
|
|
10-10-2009, 06:46 PM
|
#4
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep:
|
Quote:
Originally Posted by Ahmed
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'
|
|
|
10-10-2009, 07:14 PM
|
#5
|
Member
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893
Rep:
|
Quote:
Originally Posted by lutusp
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/) *
|
|
|
10-10-2009, 07:30 PM
|
#6
|
LQ Newbie
Registered: Oct 2009
Distribution: Gentoo
Posts: 29
Rep:
|
Quote:
Originally Posted by mariogarcia
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)
|
|
|
10-10-2009, 07:36 PM
|
#7
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep:
|
Quote:
Originally Posted by jmc1987
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)" *
|
|
|
10-10-2009, 08:12 PM
|
#8
|
Member
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 310
Rep:
|
Quote:
grep -ir <pattern_name> *
|
job done!
|
|
|
10-10-2009, 08:17 PM
|
#9
|
Member
Registered: May 2005
Location: München, Germany
Distribution: Slackware, Arch
Posts: 386
Rep:
|
Quote:
Originally Posted by lutusp
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
|
|
|
10-10-2009, 08:18 PM
|
#10
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
Code:
ls | grep "pattern"
is that it?
|
|
|
10-10-2009, 08:34 PM
|
#11
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Quote:
Originally Posted by smeezekitty
Code:
ls | grep "pattern"
is that it?
|
It will only search file names, not their contents.
|
|
|
10-10-2009, 09:07 PM
|
#12
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep:
|
Quote:
Originally Posted by Ahmed
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.
|
|
|
10-10-2009, 10:52 PM
|
#13
|
Senior Member
Registered: Sep 2007
Posts: 1,047
Rep:
|
Quote:
Originally Posted by sumeet inani
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>
|
|
|
10-12-2009, 01:15 AM
|
#14
|
Member
Registered: Oct 2008
Posts: 908
Original Poster
Rep:
|
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.
|
|
|
10-12-2009, 01:30 AM
|
#15
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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
|
|
|
All times are GMT -5. The time now is 08:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|