Questions tagged [tee]

For questions related to Linux syscall and a user program that duplicates the contents of a pipe. For security frameworks, use the tags [trusted-execution-environment] or [op-tee].

Introduction

The tee command reads from standard input and writes to standard output and files.

Examples of writing standard output include:

# print output of ps to a file
$ ps aux | tee ps_aux.txt

Using tee with sudo command

$ echo 'foo' >> file 
zsh: permission denied: file

As part of a pipe, tee can take the input and elevate permissions

$ echo "foo" | sudo tee -a file 

Links

How do I “tee” variable number of times in a bash “for loop”?

Linux and Unix tee command

536 questions
750
votes
12 answers

How do I write standard error to a file while using "tee" with a pipe?

I know how to use tee to write the output (standard output) of aaa.sh to bbb.out, while still displaying it in the terminal: ./aaa.sh | tee bbb.out How would I now also write standard error to a file named ccc.out, while still having it displayed?
jparanich
  • 8,372
  • 4
  • 26
  • 34
446
votes
32 answers

Displaying Windows command prompt output and redirecting it to a file

How can I run a command-line application in the Windows command prompt and have the output both displayed and redirected to a file at the same time? If, for example, I were to run the command dir > test.txt, this would redirect output to a file…
Ammu
244
votes
4 answers

Piping command output to tee but also save exit code of command

I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile. #!/bin/bash ... mvn clean install $@ | tee $logfile echo $? # Does not show the return code of mvn clean install Now if mvn clean install…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
169
votes
7 answers

Force line-buffering of stdout in a pipeline

Usually, stdout is line-buffered. In other words, as long as your printf argument ends with a newline, you can expect the line to be printed instantly. This does not appear to hold when using a pipe to redirect to tee. I have a C++ program, a, that…
houbysoft
  • 32,532
  • 24
  • 103
  • 156
168
votes
18 answers

How to duplicate sys.stdout to a log file?

Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls? My…
drue
  • 4,863
  • 6
  • 22
  • 14
148
votes
2 answers

linux tee is not working with python?

I made a python script which communicates with a web server using an infinite loop. I want to log every communication data to a file and also monitor them from terminal at same time. so I used tee command like this. python client.py | tee…
daehee
  • 5,047
  • 7
  • 44
  • 70
97
votes
5 answers

Capture stdout to a variable but still display it in the console

I have a bash script which calls several long-running processes. I want to capture the output of those calls into variables for processing reasons. However, because these are long running processes, I would like the output of the rsync calls to be…
Mendhak
  • 8,194
  • 5
  • 47
  • 64
87
votes
8 answers

How to replicate tee behavior in Python when using subprocess?

I'm looking for a Python solution that will allow me to save the output of a command in a file without hiding it from the console. FYI: I'm asking about tee (as the Unix command line utility) and not the function with the same name from Python…
sorin
  • 161,544
  • 178
  • 535
  • 806
68
votes
4 answers

How to tee to stderr?

I want to split stdout so that it is printed both to stdout and stderr. This sounds like a job for tee but the syntax is evading me - ./script.sh | tee stderr Of course, how should stderr actually be referred to here?
djechlin
  • 59,258
  • 35
  • 162
  • 290
40
votes
2 answers

Tee does not show output or write to file

I wrote a python script to monitor the statuses of some network resources, an infinite pinger if you will. It pings the same 3 nodes forever until it receives a keyboard interrupt. I tried using tee to redirect the output of the program to a file,…
Lily Mara
  • 3,859
  • 4
  • 29
  • 48
38
votes
3 answers

Pipe output to two different commands

Possible Duplicate: osx/linux: pipes into two processes? Is there a way to pipe the output from one command into the input of two other commands, running them simultaneously? Something like this: $ echo 'test' |(cat) |(cat) test test The reason…
Malvineous
  • 25,144
  • 16
  • 116
  • 151
36
votes
4 answers

How can I gzip standard in to a file and also print standard in to standard out?

I want to execute a command, have the output of that command get gzip'd on the fly, and also echo/tee out the output of that command. i.e., something like: echo "hey hey, we're the monkees" | gzip --stdout > my_log.gz Except when the line executes,…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
36
votes
4 answers

bash: redirect (and append) stdout and stderr to file and terminal and get proper exit status

To redirect (and append) stdout and stderr to a file, while also displaying it on the terminal, I do this: command 2>&1 | tee -a file.txt However, is there another way to do this such that I get an accurate value for the exit status? That is, if I…
rouble
  • 16,364
  • 16
  • 107
  • 102
33
votes
7 answers

Use tee (or equivalent) but limit max file size or rotate to new file

I would like to capture output from a UNIX process but limit max file size and/or rotate to a new file. I have seen logrotate, but it does not work real-time. As I understand, it is a "clean-up" job that runs in parallel. What is the right…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
28
votes
1 answer

Using tee to get realtime print statements from python

I have a python script that looks something like this: for item in collection: print "what up" #do complicated stuff that takes a long time. In bash, I run this script by doing the following: $ python my.py | tee my_file.txt However, all I…
robert
  • 1,402
  • 1
  • 15
  • 21
1
2 3
35 36