LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep (https://www.linuxquestions.org/questions/linux-newbie-8/grep-180967/)

snocked 05-13-2004 01:51 AM

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

hw-tph 05-13-2004 02:36 AM

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

snocked 05-13-2004 02:44 AM

thanks.


All times are GMT -5. The time now is 07:08 AM.