1

Possible Duplicate:
How to pipe stdout while keeping it on screen ? (and not to a output file)

For example I want to run the command:

ls -l

Then I have the output to stdout:

drwxr-xr-x 2 user user 4096 Apr 12 12:34 Desktop
-rw-rw-r-- 1 user user 1234 Apr 12 00:00 file

And I want to redirect this output to another command for some further processing (like redirecting to 'head -1' to extract first line). Can I do it in just one line?

Community
  • 1
  • 1
Lacek
  • 1,595
  • 2
  • 11
  • 30
  • 7
    `tee` will do what you want. Also, this question is better suited for http://superuser.com – George Skoptsov Apr 12 '12 at 01:47
  • @icyrock.com The first method (`tee` the output to `tty`) works for me. But the second method (using process substitution) doesn't. Anyway problem solved. Thanks! – Lacek Apr 12 '12 at 03:05

2 Answers2

3

Yes, tee will work. Something like:

ls -l | tee | head -1

To append the output to a file:

ls -l | tee -a output.txt
cheeze
  • 427
  • 5
  • 16
  • Thanks but then the output of `ls -l` doesn't appear on the console. What I want is both the output of `ls -l` and `ls -l | head -1`. – Lacek Apr 12 '12 at 03:00
0

There's a tool called tpipe which allows you to pipe a command to two other commands (like a fork), but it's not installed by default on most machines. Using it, your problem would be solved by:

ls -l | tpipe head -1
Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51