I wanted to start out this series with a command that’s included in the coreutils that I don’t think enough people utilize (or probably know about). Yes, the yes command.

Overview

Ever ran a script/command and needed to constantly hit the y key over, and over, and over, and OVER again? That’s where the yes command comes in handy. You might be thinking, oh yeah I can write this program so easily. But while the functionally of yes might be simple, the source code is by no means that. The authors took great care in pushing every piece of performance out of this application. You’ll see what I mean at the end of this article.

Usage

According to the man page:

output a string repeatedly until killed – yes(1)

By default the yes command will repeatedly print y forever. Providing arguments, the yes command will print the argument string forever.

Examples

Removing the .git directory often requires confirmation for protected files. Requiring the user to interact and confirm the deletion.

rm -r .git
override r--r--r--  user/group for .git/objects/0c/434acec682f26936fc3f529fc06f9945171c55?

Piping y to the rm command will confirm everything for you. To be fair, you can always run rm with the -f but maybe that’s not your thing.

yes | rm -r .git

If you’re running a file system consistency check and don’t want to hit y every time an error comes up.

yes | fsck /dev/sda2

If you ever need to say no constantly.

yes no
no
no
no
...

Conclusion

If you haven’t noticed, the yes command is fast. I mean really fast. If you’re running macOS or *BSD, the version is different compared to the version in GNU coreutils. They both have the same functionality, but the GNU version is incredibly fast by comparison.

Running the built in yes command on my 2016 Macbook Pro (2.9 GHz Intel Core i7 | 16GB DDR3):

yes | pv > /dev/null
258MiB 0:00:08 [32.1MiB/s]

I installed the GNU version with brew (brew install coreutils). In a way to not override the command already on macOS, all the coreutils commands prepend a g infront of the name, e.g., gyes gtail

Running the GNU version of yes:

gyes | pv > /dev/null
68GiB 0:00:08 [ 861MiB/s]

Clearly, the GNU version destorys my machine’s native command in speed. This has to do with BUFSIZ in yes's source code. I wont go over it, as kjensenxz did a fantastic job (much better than I ever could have) going over this command on reddit and showing how to maximize performance.

References