5

I know I can use tee, but I don't want the output to be printed on the screen; I want it to be printed only to the file.

Example:

ls > pk.txt 

Now, another file named praveen should also be created.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
praveen
  • 91

2 Answers2

6

Use tee(1) to write to two files and discard stdout:

ls | tee pk.txt praveen >/dev/null

Edit2: As pointed out by Stephane and Thomas, because of how tee works, this is a better version and results in less writes:

ls | tee pk.txt > praveen
jon
  • 445
  • 3
    Why prune the tee output instead of doing ls | tee pk.txt > praveen? – Stéphane Chazelas May 13 '14 at 10:19
  • "...because of how tee works, this is equivalent to: ls | tee pk.txt > praveen" Actually, while the output might be equivalent, the former oneliner leads to unnecessary writes in order to discard the output, so the behavior is not equivalent. – Thomas Nyman May 13 '14 at 10:40
2

With zsh:

ls > file1 > file2

(internally, zsh creates a pipe and spawns a process that reads from that pipe and writes to the two files as tee does. ls stdout is the other end of the pipe).