MichaelR
MichaelR

Reputation: 999

How to show command output in stdout and also transfer it to pipe

Lets say I have the fallowing cmd:

ls | grep dir

If there are folders which names contains dir then ill see them. If there arent, then I wont see any output at all.

Now, What i want is to see the output of the ls command, and then also see the final output after the grep.

Lets say something like this:

>>ls | grep dir
filea fileb filec
filed dir1  dir2

dir1 
dir2

Where the first 2 rows are the result of ls and the last 2 rows are the result of the grep command.

How do i do that?

Upvotes: 0

Views: 336

Answers (3)

rojomoke
rojomoke

Reputation: 4025

ls |tee /dev/tty |grep dir

will do that, although it won't put a space between the two parts.

Upvotes: 2

Győző Papp
Győző Papp

Reputation: 157

The simplest way is to run ls twice:

ls; ls -1 | grep dir

Please pay attention -1 option. grep is line oriented that's why ls should print one dir entry per line.

Upvotes: 0

Arnaud Potier
Arnaud Potier

Reputation: 1780

I'm cheating a bit there, but can't you just do both commands one after another? like (adding an echo to put some space between them):

 ls && echo && ls | grep dir

Upvotes: 0

Related Questions