2

I am trying to output the ps command to a file in /home/username/ps.txt. I want to create the file on the go, write the output of ps to it and also read the same in a single command. I tried this:

ps > /home/username/ps.txt | less /home/username/ps.txt

It writes to the file after creating it but does not output it. But using less seperately gives proper output.

Could I perform this in a single command?

forquare
  • 3,396
  • 5
  • 21
  • 32

2 Answers2

2

tee writes both to file and stdout , use that:

ps -ef | tee psoutput.txt | less

Or just use && to open file with less upon successful completion of ps

ps -ef > psoutput.txt && less psoutput.txt
Sergiy Kolodyazhnyy
  • 16,187
  • 11
  • 53
  • 104
  • Why && ? Shouldnt piping have been the same thing? – Bismeet singh May 04 '16 at 06:31
  • So what you do in the example you have is `command > output.txt | another_command` . You cannot do that. The `stdout` of the command won't go to pipe, but to file. It only goes one way. That's why you want to redirect first, then open file , that's why `&&` , to know when command is finished. Now, if you want to both redirect to file AND to pipe, use `tee` as I've shown – Sergiy Kolodyazhnyy May 04 '16 at 06:34
0

Just too brief. tee can be used for this purpose

ps | tee ps-info.txt