2

Grep works when I use is with ls | but when I try the command by itself, it just gives me a blinking cursor that never goes away on the next line (as if it's running a command).

For example, I tried:

ls | grep zip

while in the bin directory and got a list of files, but plain grep zip in the same directory gives me the problem stated above.

  • sorry, ill include this with the question – thinksinbinary Jan 27 '16 at 1:27
3

From man grep (emphasis mine):

grep  searches the named input FILEs (or standard input if no files are
named, or if a single hyphen-minus (-) is given as file name) for lines
containing  a  match to the given PATTERN.  By default, grep prints the
matching lines.

If you ran grep without a filename, or a pipe (the | in ls | grep):

grep foo

The standard input is the terminal - i.e., you. You have to provide the input which grep will search.

$ grep foo
this is me typing stuff
now I'm typing foo, which will get printed again because grep matched foo     
now I'm typing foo, which will get printed again because grep matched foo
2

grep takes a file or (files) as its input, when no file name is given grep reads from standard input (file descriptor 0).

When you do ls | grep 'something', you are redirecting ls command's standard output to grep standard input via pipe (that's what | does).

While in the later case when you just do grep 'something', there no file name given and hence grep will read its STDIN for input, no pipe is involved here so you need to type something on the STDIN (give input) to see the output.

Example:

% grep 46
245
480
460
460

As you can see grep matched 46 from my typed contents and showed it on STDOUT. BTW to close the interactive input session, press Ctrl + D which indicates EOF (End of File) (in this case read it as "End of Input").

  • i suppose grep is supposed to be used to search a file for content if it needs input from a file, however, weirdly enough, i tried making a file called test.txt and just typed in a bunch of random lower case letters, then used "grep test.txt *[[:lower:]]", and instead it displayed all the folders in the directory, without listing any of the other files that were in there – thinksinbinary Jan 27 '16 at 18:10
0

grep is not using for finding files in linux. You can use find command for searching files. grep command can be used to search text inside of the files.

If you want to find files named zip inside of the /bin directory use below find command find /bin -name "zip"

i used * infront of and ath the end, since the files there can be as gunzip, bzip, gzip. So to search everything matches zip word i used *.

Please read below two articles on how to use grep command and find command seperately. It would be more useful. https://screwlinux.com/how-to-use-grep-command-in-linux/ https://screwlinux.com/find-command-in-linux-with-examples/ Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.