This question already has an answer here:
I am practicing using the terminal in Mac OSX.
I went to the /dev/
folder from root, and wanted to look for the line containing null
, so I did ls
to get the folder contents. There was a lot, so I saw this as an opportunity to use grep
. So I typed ls | grep "null"
and simply got back null
instead of the full line null ttyp9
as shown in the normal ls
listing.
Why is this happening? I thought grep
returned full lines?
ttyp9
is a different file.ls
is showing you columns of output.When not sent to the terminal
ls
doesn't do that and acts as if you used the-1
argument.grep
does return full lines. Thels
command by itself does not give details. Try this instead:That listing flag gives more details, which grep can then return.
In other words,
grep
was doing what you wanted it to, there was just nothing there to see.The
ls
command without options will just show filenames. Typically the returned list of filenames is wrapped. You can unwrap them if you type:If you look at this listing, you will see that
null
is actually on a line all by itself.If you add the
long listing
option:You will see much more detail. The
grep
command can only return what is given to it, and the in this casels
is what is supplying all the inputs for grep.