5 ms·
If you ever need to write a random character to a pipe very fast, GNU coreutils has you covered with yes(1). It runs at about 6 GiB/s on my system: yes | pv
by themulticaster 4y ago
If you ever need to write a random character to a pipe very fast, GNU coreutils has you covered with yes(1). It runs at about 6 GiB/s on my system:
yes | pv > /dev/null
There's an article floating around [1] about how yes(1) is extremely optimized considering its original purpose. In care you're wondering, yes(1) is meant for commands that (repeatedly) ask whether to proceed, expecting a y/n input or something like that. Instead of repeatedly typing "y", you just run "yes | the_command".
Not sure about how yes(1) compares to the techniques presented in the linked post. Perhaps there's still room for improvement.
[1] Previous HN discussion: https://news.ycombinator.com/item?id=14542938 https://news.ycombinator.com/item?id=14542938
- dpflug 4y agoFaster still is pv < /dev/zero > /dev/null
- BenjiWiebe 4y agoYes but you don't have control of which character is written (only NULLs). yes lets you specify which character to output. 'yes n' for example to output n.
- gitgud 4y ago> It runs at about 6 GiB/s on my system... Honest question: what are the practical use cases of this? Repeatedly typing the 'y' character into a Linux pipe is surely not that common, especially at that bit rate. Also seems like the bottleneck would always be the consuming program...
- singron 4y agoYes can repeat any string, not just "y". It can be useful for basic load generation.
- jolmg 4y agoI've used it to test some db behavior with `yes 'insert ...;' | mysql ...`. Fastest insertions I could think of.
- TacticalCoder 4y ago> Repeatedly typing the 'y' character into a Linux pipe is surely not that common, especially at that bit rate. At that rate no but I definitely use it once in a while. For example if a copy quite a few files and then get repeatedly asked if I want to overwrite the destination (when it's already present). Sure, I could get my commmand back and use the proper flag to "cp" or whatever to overwrite, but it's usually much quicker to just get back the previous line, go at the beginning (C-a), then type "yes | " and be done with it. Note that you can pass a parameter to "yes" and then it repeats what you passed instead of 'y'.
- linsomniac 4y agoHistorically, you could have dirty filesystems after a reboot that "fsck" would ask an absurd number of questions about ("blah blah blah inode 1234567890 fix? (y/n)"). Unless you were in a very specific circumstance, you'd probably just answer "y" to them. It could easily ask thousands of questions though. So: "yes | fsck" was not uncommon.
- jolmg 4y ago> Historically It's probably still common in installation scripts, like in Dockerfiles. `apt-get install` has the `-y` option, but it would be useful for all other programs that don't.
- linsomniac 4y agoJust to clarify: I was applying "historically" to "fsck", not to the use of "yes" in general. I can't remember the last time I've had the need to use "yes | fsck"