Redirect output to multiple processes
November 8th, 2006 toydi Posted in Common, grep, process substitution, tee | Hits: 156944 | 46 Comments »
Since tee can read the standard input, and write to multiple files, we may leverage this feature so that it writes to multiple processes (instead of files).
tee >(process1) >(process2) >(process3) | process4
>( ) (see process substitution) pretends itself as a write-only file. Everything you write into it, will be passed to the command (as standard input) within its parenthesis.
ls -A | tee >(grep ^[.] > hidden-files) >(grep -v ^[.] > normal-files) | less
So in this example (rather useless though), it will list all the filenames, then send the listing to 3 processes:
grep ^[.] > hidden-files– save hidden filenames to a file.grep -v ^[.] > normal-files– save non-hidden filenames to another file.less– display all filenames in a pager.







April 27th, 2007 at 6:49 pm
How to redirect the o/p to a file in runtime.
For example Here Run time means.. i’m trying to compile a project, in that compilation it takes input parameters in the runtime i.e after executing some steps it asks the user input to proceed further for compiling.. In this case how to get the output of the compilation into a logfile for postpartum purpose.
April 28th, 2007 at 4:37 am
Are you using make to do the compilation? or running a bash script?
For normal compilation, usually come with ./configure;make;make install;
I assume the compilation of your project is by running bash script such as install.sh, so that It is possible to ask for user input.
If that is the case, I suggest you to read the scripts and modify that line that you are interested to log.
Or, simple way, log entire process from start to the end, such as
May 17th, 2007 at 2:30 pm
Hi folks,
Please help me to find a command so that i can redirect the output of below command to a file.
# top -d 1 | grep “init”
May 17th, 2007 at 6:58 pm
Ambika Prasad:
Interesting, you can do this
May 23rd, 2007 at 8:39 pm
how to get the kernal functions addresses in symbol table?
July 19th, 2007 at 6:54 pm
[...] Redirect output to multiple processes » Linux by Examples (tags: linux) [...]
September 4th, 2007 at 8:26 pm
Hi, could you help me how to redirect the first 5 lines of the “top” command into a file….
October 5th, 2007 at 1:03 am
This will get you the first 5 lines out of top, then top will end.
top -n 1 -b -d 1 | tee >(sed -n ’1,5 p’ > test ) >/dev/null
October 30th, 2007 at 5:06 am
Can you tell me the command in LS which lists only directories…
October 30th, 2007 at 7:56 am
Mahesh:
How to list just directories ( the correct way)
November 1st, 2007 at 5:15 pm
I get this error in buildroot but not on my native system:
wget -O – ftp://192.168.3.200/something.tgz |tee >(/bin/tar xzf – ) |md5sum
-sh: syntax error near unexpected token `(‘
I tried to change from Ash to Bash with no result.
Now I’m searching for the sources of tee and will real tee instead of the busybox tee.
November 5th, 2007 at 10:54 pm
Looks like busybox ash has troubles with process substitution like >() and wants $() instead. But then the data piped to the process is not valid…
Aargh…
I also tried it with bash, then I can use >() but data piped to the process is also invalid…
November 6th, 2007 at 1:18 am
JeroenSt: heh! tee >() sometimes is really hard to play with, it sometimes just doesn’t act what I ask for. I will suggest you to look into python. There is a os.popen() which can run any commands, where the result will be stored into file object, so you can get the results from .readlines() and manipulate as much as you can.
Recently, I look into python, that is really convenient, will probably post about python later on when I have experience more.
November 6th, 2007 at 9:16 pm
We don’t have python at our embedded device, but I figured out another way:
mknod /tmp/pipe1 p
mknod /tmp/pipe2 p
wget -q -O – ftp://192.168.3.200/somefile.tgz |tee /tmp/pipe1 /tmp/pipe2 |>/dev/null&
md5sum /tmp/checksum&
tar -xzf – -C /tmp/somedir
November 6th, 2007 at 9:18 pm
Hmmz, the rest of my article is not saved!?
But the pipe output of tee is trashed because it is not valid somehow.
The other problem is that md5sum and tar will wait forever if tee doesn’t start (out of memory or corrupt)
November 6th, 2007 at 11:04 pm
JeroenSt:
Actually, I am not sure what are you trying to do.
Anyway, How about split it to few steps?
wget -q -O /tmp/tmp.tgz ftp://192.168.3.200/somefile.tgz;md5sum /tmp/tmp.tgz; tar -zxvf /tmp/tmp.tgz -C /tmp/somedir;
Not sure why you wanna checksum…
November 7th, 2007 at 11:15 am
Can I increase the run levels more than 6?If it is possible how can we do that and how many maximum run levels can we assign?
November 7th, 2007 at 3:30 pm
Well, the reason why I do this is because I only have a limited space of flash on the embedded device. When I download the file on flash first, I can’t fill the flash (because 1/3 is allready used by the tgz file). The md5sum is also on the FTP server, so I can be sure that the transfer was correct and also was the right file.
November 7th, 2007 at 4:58 pm
JeroenSt:
Emm… You may get help, if you ask at irc://irc.freenode.net/bash.
A lots of expert over there.
January 11th, 2008 at 4:14 pm
Think I understand how it works, at least for Bash:
# echo hello >(cat >testfile)
Returns: hello /dev/fd/63
This is a temporary file descriptor, that is connected to the STDIN of a process started executing ‘cat >testfile’.
If the echo command ends, the file descriptor is removed.
So the construction >(command) is replaced by a filename.
If you execute:
echo ‘hello’ | tee >(wc >wordcount) >(md5sum >sum)
A file descriptor is created for the ‘wc >wordcount’, for exampe ‘/dev/fd/63′, and a file descriptor is created for the ‘md5sum >sum’, for example ‘/dev/fd/62′.
The command that is executed after the creation of the file descriptors is:
echo ‘hello’ | tee /dev/fd/63 /dev/fd/62
And I see it works, I get a file named ‘wordcount’ and a file named ‘sum’.
January 12th, 2008 at 1:39 am
Jeroen:
Thanks for the great explanation. I find tee sometimes is very hard to understand, but once you got it right, it will be very useful.
February 1st, 2008 at 6:27 pm
Can anyone help me with this?
I have one script which is running from last 15 days in the background.Now My project manager wants the few output of that running process without stopping it.
Pls Help
February 3rd, 2008 at 5:43 am
Ajeet: You may try fg to bring it to front, and ctrl+z to bring it back to background. But I am not sure whether it works for your case.
February 3rd, 2008 at 6:30 am
Hai to all!!
I want to install the drivers of video card on fedora but I am unable to find the graphics card installed in my pc .FYI ,PC is not booting into graphical mode..Help me out in this issue.
Thanks
August 25th, 2008 at 5:52 pm
Hi,
I have to redirect the output of a time function of a command which also redirects also its out output to a log file:
time athena.py jobOptions.pythia.py | tee athena_gen.out
Output of athena.py is already going to athena_gen.out but I would like to redirect the output of time to a file too..
Thanks,
Omer
August 27th, 2008 at 7:04 pm
Since time is actually a built in bash function, it handles it differently to usual commands. Bash, however, is quite an intelligent shell, allowing for redirection of its own functions (although time doesn’t work like the others (echo for example) for some reason). The solution is to parenthesize the time function and its arguments, then have that redirected to a file:
(time do_something) 2>result.time
February 5th, 2009 at 1:44 am
[...] Redirect output to multiple processes Since tee can read the standard input, and write to multiple files, we may leverage this feature so that it writes to mu… [...]
February 25th, 2009 at 5:54 am
Hi,
I want to tee into two process but keep fail.
My linux system might be very old.
tee –version -> 2.0 (C) 1999
When I tried to
echo “345″ | tee >(grep 5)
it show a syntax error “(” unexpected.
But I could do (…) | ….
So is it because of the tee?
Any suggestion or work around?
January 15th, 2012 at 1:55 am
[...] This example comes from this article. [...]
September 14th, 2012 at 10:59 pm
[...] simple AND visually appealing. Beautiful. See Wiki link and by examples link. This entry was posted in 8/10, bash shell by admin. Bookmark the [...]
January 6th, 2013 at 10:34 am
It should be noted that this ability is quite specific to bash. Most other shells do not support this ability. Thanks for the great article
March 13th, 2013 at 12:31 pm
Great article! Thanks~
April 28th, 2013 at 11:05 pm
[...] simple AND visually appealing. Beautiful. See Wiki link and by examples link. Share this:TwitterFacebookLike this:Like Loading… This entry was posted in 8/10, bash shell by [...]
July 7th, 2013 at 2:02 am
[...] source Answered by Ben [...]
June 28th, 2014 at 5:44 am
Having read this I thought it was extremely informative.
I appreciate you spending some time and energy to put
this content together. I once again find myself spending a lot of time both reading and posting
comments. But so what, it was still worthwhile!
August 24th, 2014 at 12:14 pm
Very good post! We will be linking to this particularly
great article on our website. Keep up the great writing.
Here iss my homepage; Nitro Focus No3 free trial
September 25th, 2014 at 9:52 am
Hello There.I found your blog using msn. This
is aan extremely wesll written article. I’ll make sure to bookmark it and
come back to rad more of your usefjl information. Thanks
for the post. I will definitely comeback.
September 25th, 2014 at 11:27 pm
Hi there to every one, the contents existing at this site are really remarkable for people experience, well, keep up the good work fellows.
September 30th, 2014 at 12:59 am
Great post. I’m experiencing some of these issues as well..
October 22nd, 2014 at 4:10 am
Howdy very cool website!! Man .. Excellent .. Wonderful ..
I’ll bookmark your web site and take the feeds additionally?
I am satisfied to find a lot of useful information right here
in the post, we need work out extra techniques on this
regard, thanks for sharing. . . . . .
???????????????????????????:???????
October 22nd, 2014 at 5:28 am
Hi, every time i used to check web site posts here early in the
morning, since i enjoy to find out more
and more.
?????-????????
October 31st, 2014 at 6:52 am
Other Icaros call the spirit of Ayahuasca to open visions as though
exposing the optic nerve to light’. If I had those two in my life, I settled much faster and I felt happy.
The acai berries must be loaded into baskets and onto boats soon after picking.
Moonstone Soothes and balances the emotions; helps eliminate
fear of “feeling”; encourages inner growth and strength; aids peace
and harmony and psychic abilities; aligns vertebrae; digestive
aid. Inhalation: You can either apply the oil directly to
your hands, rub them together and then cup your hands over your
nose and inhale deeply.
December 29th, 2014 at 3:57 am
[...] source Source [...]
March 9th, 2015 at 7:57 pm
[...] source Source [...]
May 16th, 2015 at 1:04 pm
This piece of writing provides clear idea in favor of the new users
of blogging, that truly how to do running a blog.
November 21st, 2015 at 10:49 am
You ought to be a part of a contest for one
of the greatest blogs on the web. I am going to recommend this blog!