LinuxQuestions.org
Visit Jeremy's Blog.
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 05-13-2004, 02:51 AM   #1
snocked
Member
 
Registered: Dec 2002
Location: St. Louis, MO
Distribution: Slackware 9.1
Posts: 482

Rep: Reputation: 30
grep


I've never been able to understand this command. It searches dir/files for a file pattern.

grep [-cilnv] pattern [filelist]

how do you use the grep command with ls? Let's say I wanted to search through an entire
directory of files for a pattern of text. how is this done?

the following does not work:
ls location | grep -l pattern

Last edited by snocked; 05-13-2004 at 02:55 AM.
 
Old 05-13-2004, 03:36 AM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Do you want to search the list of filenames that the ls command returns, or the contents of the files themselves?

To list the contents of your ~/downloads directory and look for occurances of the string "bleh" in the filenames you could do something like this: ls ~/downloads | grep "bleh"

To go through the files in a directory and display what files contain the string "bleh", and in what file and on what line, you could type this: grep -Hn bleh *
The -H switch displays the filename, the -n switch displays line number in the file(s) where "bleh" is found, "bleh" is the string to search for and * is the mask for the files to be searched (an asterisk meaning all files).

To recursively look for files containing the string "bleh" in their name, try this: find . -name "*bleh*"


Håkan
 
Old 05-13-2004, 03:44 AM   #3
snocked
Member
 
Registered: Dec 2002
Location: St. Louis, MO
Distribution: Slackware 9.1
Posts: 482

Original Poster
Rep: Reputation: 30
thanks.
 
  


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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
help on grep!!!! sanjith11 Programming 5 01-20-2005 06:43 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 02:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:50 AM.

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