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.
From
man grep
(emphasis mine):If you ran
grep
without a filename, or a pipe (the|
inls | grep
):The standard input is the terminal - i.e., you. You have to provide the input which
grep
will search.grep
takes a file or (files) as its input, when no file name is givengrep
reads from standard input (file descriptor 0).When you do
ls | grep 'something'
, you are redirectingls
command's standard output togrep
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 hencegrep
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:
As you can see
grep
matched46
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").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.