On 1/21/21 4:11 PM, Pádraig Brady wrote:
On 21/01/2021 13:17, Alejandro Colomar wrote:
This is useful for using tee to just write to a file,
at the end of a pipeline,
without having to redirect to /dev/null
Example:
echo 'foo' | sudo tee -q /etc/foo;
is equivalent to the old (and ugly)
echo 'foo' | sudo tee /etc/foo >/dev/null;
This was discussed last month:
https://lists.gnu.org/archive/html/coreutils/2020-12/msg00037.html
thanks,
Pádraig
Hi all,
I'd like to continue the discussion you had in that thread:
The task we are trying to improve is:
writing stdin to a file (either truncating(overwriting) or appending).
Commonly used tools:
cat, tee, dd
- cat, as Padraig pointer out doesn't have an option to append.
- dd doesn't either (AFAIK).
- dd is quite dangerous, and I would avoid it for simple tasks.
- I read on stackexchange (I couldn't find the thread now)
that the dd performance was lower, but I don't remember well.
- 'sh -c "exec cat > file" is even uglier than 'tee file >/dev/null'
tee is the only tool that already has the options ('-a') to precisely
write into a file the way you want. cat can do the same with proper
redirection from the shell (>> / >), but I'd argue against having two
completely different expressions for doing exactly the same thing,
depending on if you need sudo or not:
cat > file; -> sudo dd of=file status=none;
cat >[>] file; -> sudo sh -c "exec cat >[>] file";
cat >[>] file; -> sudo tee [-a] file >/dev/null;
tee [-a] file >/dev/null; -> sudo tee [-a] file >/dev/null;
tee -q[a] file; -> sudo tee -q[a] file;
I've sorted them from worst to best IMO.
Don't you think its quite natural to do use tee everywhere?
BTW, what a coincidence that you discussed this a month ago! :-)
Cheers,
Alex
--
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/