Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
30
votes
3
answers
3596
views
Why does 'ping' not output a summary when redirecting output?
I can ping *google.com* for several seconds and when I press Ctrl + C , a brief summary is displayed at the bottom: $ ping google.com PING google.com (74.125.131.113) 56(84) bytes of data. 64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=2 ttl=56 time=46.7 ms 64 bytes from lu-in-f113.1e...
I can ping *google.com* for several seconds and when I press Ctrl + C, a brief summary is displayed at the bottom:
$ ping google.com
PING google.com (74.125.131.113) 56(84) bytes of data.
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=2 ttl=56 time=46.7 ms
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=3 ttl=56 time=45.0 ms
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=4 ttl=56 time=54.5 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3009ms
rtt min/avg/max/mdev = 44.965/48.719/54.524/4.163 ms
However, when I do the same redirecting output to log file with
tee
, the summary is not displayed:
$ ping google.com | tee log
PING google.com (74.125.131.113) 56(84) bytes of data.
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=1 ttl=56 time=34.1 ms
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=2 ttl=56 time=57.0 ms
64 bytes from lu-in-f113.1e100.net (74.125.131.113): icmp_seq=3 ttl=57 time=50.9 ms
^C
Can I get the summary as well when redirecting output with tee
?
ks1322
(1670 rep)
Jan 22, 2022, 11:03 AM
• Last activity: Jun 29, 2025, 06:11 PM
5
votes
2
answers
294
views
Duplicate stdout to pipe into another command with named pipe in POSIX shell script function
``` mkfifo foo printf %s\\n bar | tee foo & tr -s '[:lower:]' '[:upper:]' <foo wait rm foo ``` This is a working POSIX shell script of what I want to do: - `printf %s\\n bar` is symbolic for an external program producing stdout - `tr -s '[:lower:]' '[:upper:]'` is symbolic for another command that i...
mkfifo foo
printf %s\\n bar | tee foo &
tr -s '[:lower:]' '[:upper:]'
This is a working POSIX shell script of what I want to do:
- printf %s\\n bar
is symbolic for an external program producing stdout
- tr -s '[:lower:]' '[:upper:]'
is symbolic for another command that is supposed to receive the stdout and do something with it
- tee
duplicates stdout to named pipe foo
And the output is as expected:
bar
BAR
Now I'd like to tidy up the code so it becomes external_program | my_function
. Something like this:
f() (
mkfifo foo
tee foo &
tr -s '[:lower:]' '[:upper:]'
But now there is no output at all.
finefoot
(3554 rep)
Mar 9, 2025, 01:34 PM
• Last activity: Mar 9, 2025, 03:19 PM
3
votes
1
answers
1285
views
How to pipe output of command to two separate commands and store outputs
I have a really long command that runs over a huge file and I am forced to run it twice which doubles the time it takes to run. This is what I am doing at the moment: ``` x=$(command | sort -u) y=$(command | sort -n) ``` I was wondering whether there is any way to redirect the output of command to b...
I have a really long command that runs over a huge file and I am forced to run it twice which doubles the time it takes to run.
This is what I am doing at the moment:
x=$(command | sort -u)
y=$(command | sort -n)
I was wondering whether there is any way to redirect the output of command to both -u
and -n
and store output of each into separate variables or files like I did above with
and
.
I tried to use tee to do the following but no luck:
command | tee >(sort -n > x.txt) >(sort -u > y.txt)
I tried to redirect output to text files but it just printed it to standard output instead.
Any tips or ideas?
markovv.sim
(35 rep)
Oct 27, 2020, 07:44 PM
• Last activity: Feb 25, 2025, 06:09 PM
2
votes
2
answers
472
views
what is the meaning and usage of 2>&1 | tee /dev/stderr
for the following command output=$(cat $file | docker exec -i CONTAINER COMMAND 2>&1 | tee /dev/stderr) what is the meaning and usage of 2>&1 | tee /dev/stderr? I google and 2>&1 means "combine stderr and stdout into the stdout stream." tee mean "reads from standard input and writes to standard outp...
for the following command
output=$(cat $file | docker exec -i CONTAINER COMMAND 2>&1 | tee /dev/stderr)
what is the meaning and usage of 2>&1 | tee /dev/stderr?
I google and 2>&1 means "combine stderr and stdout into the stdout stream."
tee mean "reads from standard input and writes to standard output and one or more files simultaneously."
but I cannot understand if combine
2>&1 | tee /dev/stderr
1) this combine stderr and stdout into the stdout stream, but then again write it to /dev/stderr (&2) again?
2) why need to combine to stdout, then redirect back to stderr?
3) and after write to /dev/stderr, will the message save to variable output in the following?
output=$(cat $file | docker exec -i CONTAINER COMMAND 2>&1 | tee /dev/stderr)
user1169587
(133 rep)
Feb 19, 2025, 07:35 AM
• Last activity: Feb 20, 2025, 09:06 AM
61
votes
7
answers
47960
views
using tee to output intermediate results to stdout instead of files
I know that to capture a pipeline's contents at an intermediate stage of processing, we use tee as `ls /bin /usr/bin | sort | uniq | tee abc.txt | grep out` , but what if i don't want to redirect the **contents after uniq to abc.txt** *but* **to screen**(through stdout, ofcourse) so that as an end r...
I know that to capture a pipeline's contents at an intermediate stage of processing, we use tee as
ls /bin /usr/bin | sort | uniq | tee abc.txt | grep out
, but what if i don't want to redirect the **contents after uniq to abc.txt** *but* **to screen**(through stdout, ofcourse) so that as an end result , **i'll have on screen, the intermediate contents after uniq as well as the contents after grep.**
Aman
(1181 rep)
Jan 12, 2015, 05:41 PM
• Last activity: Oct 11, 2024, 09:55 AM
86
votes
7
answers
161669
views
how to redirect output to multiple log files
How to redirect standard output to multiple log files? The following does not work: some_command 1> output_log_1 output_log_2 2>&1
How to redirect standard output to multiple log files?
The following does not work:
some_command 1> output_log_1 output_log_2 2>&1
doubledecker
(1897 rep)
Jun 21, 2012, 05:49 AM
• Last activity: Sep 12, 2024, 02:53 PM
1
votes
1
answers
217
views
bash: script running in pm2 unable to access file descriptors files at /dev/fd/
I have script `script.sh`: ```bash #!/usr/bin/env bash # pm2 seems to always run in `bash` regardless of `#!` echo "running in $(readlink -f /proc/$$/exe)" # Redirect to both stdout(1) and stderr(2) echo "hello" | tee /dev/fd/2 ``` I am running it with `pm2 start script.sh` I can see logs at `pm2 lo...
I have script
script.sh
:
#!/usr/bin/env bash
# pm2 seems to always run in bash
regardless of #!
echo "running in $(readlink -f /proc/$$/exe)"
# Redirect to both stdout(1) and stderr(2)
echo "hello" | tee /dev/fd/2
I am running it with pm2 start script.sh
I can see logs at pm2 logs
like so:
tee: /dev/fd/2: No such device or address
Which means script is unable to access /dev/fd
. Although this problem doesn't happen when directly running in a bash terminal.
---
- About pm2
: https://pm2.keymetrics.io/docs/usage/quick-start/
- Previous discussion: https://stackoverflow.com/questions/78885319/bash-redirecting-to-multiple-file-descriptors
- For reference : https://unix.stackexchange.com/questions/26926/file-descriptors-and-dev-fd
Amith
(313 rep)
Aug 23, 2024, 11:08 AM
• Last activity: Sep 3, 2024, 10:24 AM
24
votes
3
answers
6353
views
tee stdout to stderr?
I'd like to send stdout from one process to the stdin of another process, but also to the console. Sending stdout to stdout+stderr, for instance. For example, I've got `git edit` aliased to the following: git status --short | cut -b4- | xargs gvim --remote I'd like the list of filenames to be sent t...
I'd like to send stdout from one process to the stdin of another process, but also to the console. Sending stdout to stdout+stderr, for instance.
For example, I've got
git edit
aliased to the following:
git status --short | cut -b4- | xargs gvim --remote
I'd like the list of filenames to be sent to the screen as well as to xargs
.
So, is there a tee
-like utility that'll do this? So that I can do something like:
git status --short | \
cut -b4- | almost-but-not-quite-entirely-unlike-tee | \
xargs gvim --remote
Roger Lipscombe
(1780 rep)
Apr 30, 2014, 09:10 AM
• Last activity: Aug 11, 2024, 09:13 AM
10
votes
3
answers
31709
views
pipe password to `sudo` and other data to `sudo`ed command
Both of these commands work: (note the `-S` in `sudo` tells sudo to read the password from stdin). echo 'mypassword' | sudo -S tee -a /etc/test.txt &> /dev/null echo -e '\nsome\nmore\ntext' | sudo tee -a /etc/test.txt &> /dev/null Now I would like to combine the two, i.e. achieve everything in just...
Both of these commands work: (note the
-S
in sudo
tells sudo to read the password from stdin).
echo 'mypassword' | sudo -S tee -a /etc/test.txt &> /dev/null
echo -e '\nsome\nmore\ntext' | sudo tee -a /etc/test.txt &> /dev/null
Now I would like to combine the two, i.e. achieve everything in just one line. But, of course, something like this doesn't work:
echo -e '\nsome\nmore\ntext' | echo 'mypassword' | sudo -S tee -a /etc/test.txt &> /dev/null
What would work? Thanks:) - Loady
PS: Minor unrelated question: is 1> identical to > ? I believe they are..
Anthony Webber
(455 rep)
Sep 12, 2017, 12:46 PM
• Last activity: Aug 10, 2024, 08:42 PM
0
votes
1
answers
79
views
How can I get stderr from mbuffer written to log file when using tee?
Having a bash script that essential only doing: ``` mbuffer --md5 -i dummy.tar -o /dev/null ``` A user of this script wants to use tee and store the output messages in a log file. ``` ./script.sh 2>&1 | tee log.txt ``` Everything is visible on terminal but nothing is written to log.txt. How can I do...
Having a bash script that essential only doing:
mbuffer --md5 -i dummy.tar -o /dev/null
A user of this script wants to use tee and store the output messages in a log file.
./script.sh 2>&1 | tee log.txt
Everything is visible on terminal but nothing is written to log.txt.
How can I do to get output on both terminal and in file?
Just running in a terminal winds up the same, nothing in log.txt:
mbuffer --md5 -i dummy.tar -o /dev/null 2>&1 | tee log.txt
Tried on Ubuntu and Rocky Linux.
Also tried
stdbuf -e 0 -o 0 mbuffer --md5 -i dummy.tar -o /dev/null 2>&1 | tee log.txt
TopCatSE
(3 rep)
Jul 16, 2024, 09:41 AM
• Last activity: Jul 18, 2024, 10:59 AM
0
votes
1
answers
123
views
can tee 'save' to stderr?
I know how to use `tee`, of course, I have used it many times. However, it is normally something like: ``` du -h /some/directory | sort -h | tee census ``` While this works fine, it means I don't see anything until almost near the end, because `sort` needs to collect the whole list of output from `d...
I know how to use
tee
, of course, I have used it many times. However, it is normally something like:
du -h /some/directory | sort -h | tee census
While this works fine, it means I don't see anything until almost near the end, because sort
needs to collect the whole list of output from du
. What I'm thinking of could be good is if it is possible to do something along the lines of:
du - h | tee stderr | sort -h > census
where obviously stderr
would be some notation to represent the real error output channel. I haven't found anything in the rather sparse documentation - is it it possible?
j4nd3r53n
(779 rep)
May 28, 2024, 10:59 AM
• Last activity: May 28, 2024, 12:24 PM
0
votes
1
answers
183
views
Use grep to search stdout, without replacing stdout
At the moment, I have this in a wrapper script: ``` 2>&1 ./update.sh | ts | tee -a ./update.log ``` and this in update.sh: ``` if apt full-upgrade -y | grep linux-headers then echo echo Need to Fix the Capture Driver! fi ``` (there's some additional logic to fix the driver automatically, but I think...
At the moment, I have this in a wrapper script:
2>&1 ./update.sh | ts | tee -a ./update.log
and this in update.sh:
if apt full-upgrade -y | grep linux-headers
then
echo
echo Need to Fix the Capture Driver!
fi
(there's some additional logic to fix the driver automatically, but I think you get the idea)
It works, except that I don't get the full output of apt full-upgrade
in the terminal or in the logfile. I only get what grep
matches.
I could probably tee
the full output of apt full-upgrade
to a temp file, before the if
, and then grep
and rm
the temp file, but is there a better way than that to get a complete terminal and log, and keep the logic?
AaronD
(153 rep)
Apr 28, 2024, 06:39 PM
• Last activity: Apr 28, 2024, 08:04 PM
8
votes
1
answers
1101
views
What is the '.tee_history' file?
This is a plaintext file, apparently limited to 300 lines. I use `tee` frequently on my system. $ file .tee_history .tee_history: Unicode text, UTF-8 text $ wc -l .tee_history 300 .tee_history $ tee --version tee (GNU coreutils) 9.3 --- I have no use for the history. Can this logging be easily disab...
This is a plaintext file, apparently limited to 300 lines. I use
tee
frequently on my system.
$ file .tee_history
.tee_history: Unicode text, UTF-8 text
$ wc -l .tee_history
300 .tee_history
$ tee --version
tee (GNU coreutils) 9.3
---
I have no use for the history. Can this logging be easily disabled or would periodically truncating (> .tee_history
) or removing the file be the most sensible solutions?
user598527
(735 rep)
Mar 17, 2024, 09:28 AM
• Last activity: Mar 17, 2024, 06:49 PM
2
votes
1
answers
144
views
How to "render" ouput from a command playing with tput so only the final terminal-postprocessed result is kept?
I captured the output of a script that uses tput to draw certain things on screen. When I perform cat myoutput then everything is well seen (looks like terminal reinterprets it from beginning), but when I edit or pipe that output I see plenty of ansi sequences and all the stuff previous to destructi...
I captured the output of a script that uses tput to draw certain things on screen. When I perform cat myoutput then everything is well seen (looks like terminal reinterprets it from beginning), but when I edit or pipe that output I see plenty of ansi sequences and all the stuff previous to destructive printing like tput clear and the like.
How can I a postprocess it so I only get the final "render"?
Even better, the origin of this is that I am currently teeing my script so it prints everything to a file aside from to the terminal
with
exec > >(tee /dev/tty)
is there a way to tell the stdout channel to "render" everything before saving?
Whimusical
(285 rep)
Mar 16, 2024, 09:41 AM
• Last activity: Mar 16, 2024, 12:10 PM
0
votes
1
answers
143
views
Why is tee stopping in the middle when piped with nohup?
I'm running a script (in a Laravel project) that takes a few hours to complete. `nohup` to prevent it from exiting if my ssh session is disconnected. ```lang-shell nohup php artisan do:thing 2>&1 | tee ~/tmp ``` It starts off nicely, but after some time, I get a broken pipe error and it stops output...
I'm running a script (in a Laravel project) that takes a few hours to complete.
nohup
to prevent it from exiting if my ssh session is disconnected.
-shell
nohup php artisan do:thing 2>&1 | tee ~/tmp
It starts off nicely, but after some time, I get a broken pipe error and it stops outputing things, and tee
stops as well - the doc stops at that point.
I guess it's because the tee
process got killed by the ssh session drop? Can I prevent it?
Could I maybe disown the tee process and prevent ssh from terminating it?
Jeremy Belolo
(103 rep)
Jan 11, 2024, 07:16 AM
• Last activity: Jan 11, 2024, 08:38 AM
-1
votes
1
answers
84
views
What does the tee l2r or r2l argument mean?
I saw a socat example [here][1], where they use tee l2r | socat - "TCP:remote:5555" | tee r2l what does the `l2r` or `r2l` mean? [1]: https://gist.github.com/mario21ic/c09f0a648130ad6a91abdde41cb011c8#file-socat_examples-L43
I saw a socat example here , where they use
tee l2r | socat - "TCP:remote:5555" | tee r2l
what does the
l2r
or r2l
mean?
René Link
(131 rep)
Dec 12, 2023, 04:02 PM
• Last activity: Dec 13, 2023, 03:59 PM
18
votes
3
answers
13392
views
What is the `tee` command in Linux?
I am trying to interpret the following `tee` command: `cat colors.txt words.txt | tee colorsAndWords.txt | wc` Is my understanding, as follows, correct? 1. `cat colors.txt words.txt`: This command concatenates the contents of the `colors.txt` and `words.txt` files and sends the combined output to th...
I am trying to interpret the following
tee
command:
cat colors.txt words.txt | tee colorsAndWords.txt | wc
Is my understanding, as follows, correct?
1. cat colors.txt words.txt
: This command concatenates the contents of the colors.txt
and words.txt
files and sends the combined output to the standard output (the terminal).
2. | tee colorsAndWords.txt
: The |
(pipe) symbol takes the output of the previous command and passes it as input to the tee
command. tee
is used to both display the data on the standard output (usually the terminal) and write it to a file. In this case, it writes the concatenated output to a file named colorsAndWords.txt
.
3. | wc
: The final | wc
takes the output of the tee
command, which is still the concatenated content, and passes it to the wc
command. wc
is used to count the number of lines, words, and characters in the text it receives as input.
Nosail
(281 rep)
Oct 26, 2023, 09:47 AM
• Last activity: Oct 28, 2023, 08:05 PM
0
votes
1
answers
78
views
Can I use tee to display output that other utilities tend to suppress?
The macos `pbcopy` utility grabs its input stream and stores it in the system clipboard, without displaying anything. So, when I want to copy/paste the output of a command in the terminal, I typically first run a command to print normally to see what I'm getting then run again to pipe to pbcopy. Sim...
The macos
pbcopy
utility grabs its input stream and stores it in the system clipboard, without displaying anything. So, when I want to copy/paste the output of a command in the terminal, I typically first run a command to print normally to see what I'm getting then run again to pipe to pbcopy.
Similar utilities exist on Linux, IIRC. Can tee
be used to send data to pbcopy, rather than a file, thus allowing a display at the same as the copy?
Example:
For whatever reason, I want to document something about files in a directory, but only list files with a
or b
characters in their name.
The first ls
is used to make sure I've filtered out what I don't want. The second is used to put the results in my clipboard. Can I do everything in one step, by somehow leveraging tee
?
`
% touch a b c
% ls | egrep 'a|b'
a
b
% ls | egrep 'a|b' | pbcopy
%
`
##### clipboard content, after the 2nd run, which I would prefer in one step:
`
a
b
`
JL Peyret
(115 rep)
Oct 27, 2023, 06:56 PM
• Last activity: Oct 27, 2023, 07:05 PM
14
votes
4
answers
5267
views
Is there a standard alternative to sponge to pipe a file into itself?
I frequently want to do something like this: cat file | command > file (which obviously doesn't work). The only solution I've seen for this is `sponge`, i.e. cat file | command | sponge file Unfortunately, `sponge` is not available to me (nor can I install it or any other package). Is there a more s...
I frequently want to do something like this:
cat file | command > file
(which obviously doesn't work). The only solution I've seen for this is
sponge
, i.e.
cat file | command | sponge file
Unfortunately, sponge
is not available to me (nor can I install it or any other package).
Is there a more standard quick way to do this without having to break it up every time into multiple commands (pipe to temp file, pipe back to original, delete temp file)? I tried tee
for example, and it seems to work, but is it a consistent/safe solution?
argentum2f
(303 rep)
Sep 24, 2019, 10:45 PM
• Last activity: Oct 20, 2023, 01:31 PM
2
votes
2
answers
285
views
tee /dev/stderr inside script truncates output file if you redirect stderr
Given a script like below if I try to redirect stderr to a file, then the file is truncated at the point `tee` is used: ``` $ cat test.sh #!/bin/bash set -eux echo before echo '{ "foo": "bar" }' | tee /dev/stderr | jq .foo echo after $ ./test.sh 2> log before "bar" after $ cat log { "foo": "bar" } +...
Given a script like below if I try to redirect stderr to a file, then the file is truncated at the point
tee
is used:
$ cat test.sh
#!/bin/bash
set -eux
echo before
echo '{ "foo": "bar" }' | tee /dev/stderr | jq .foo
echo after
$ ./test.sh 2> log
before
"bar"
after
$ cat log
{ "foo": "bar" }
+ echo after
The log
file should have all of the stderr output. Everything I see if I run the same script without redirecting:
$ ./test.sh
+ echo before
before
+ echo '{ "foo": "bar" }'
+ tee /dev/stderr
+ jq .foo
{ "foo": "bar" }
"bar"
+ echo after
after
So why do I only see the lines that come _after_ tee
? Using 2>>
doesn't seem to help.
I Don't understand why is this happening. How can I tee the output while still having the ability to redirect the whole script stderr to a file?
Jakub Bochenski
(325 rep)
Oct 13, 2023, 05:06 PM
• Last activity: Oct 13, 2023, 09:55 PM
Showing page 1 of 20 total questions