Reputation: 591
I'm trying to unzip a file and redirect the output to a named pipe and another file. Therefore I'm using the command tee
.
gunzip -c $FILE | tee -a $UNZIPPED_FILE > $PIPE
My question is is there any other option to achieve the same but with a command that will write the file asynchronously. I want the output redirected to the pipe immediately and that the writing to the file will run in the background by teeing the output to some sort of buffer.
Thanks in advance
Upvotes: 2
Views: 2211
Reputation: 9282
What you need is a named pipe (FIFO). First create one:
mkfifo fifo
Now we need a process reading from the named pipe. There's an old unix utillity called buffer
that was earlier for asynchronous writing to tape devices. Start a process reading from the pipe in the background:
buffer -i fifo -o async_file -u 100000 -t &
-i
is the input file and -o
the output file. The -u
flag is only for you to see, that it is really asynchonous. It's a small pause after every write for 1/10 second. And -t
gives a summary when finished.
Now start the gunzip
process:
gunzip -c archive.gz | tee -a fifo > $OTHER_PIPE
You see the gunzip process is ending very fast. In the folder you will see the file async_file
growing slowly, that's the background process that is writig to that file, the buffer
process. When finishing (can take very long with a huge file) you see a summary. The other file is written directly.
Upvotes: 1