yes command in Linux with examples

The yes command in Linux will automatically output a “y” or any string you specify, repeatedly. It’s one of the simplest commands on Linux, and one that most users will find they never have much use for. But then when you do need it, you’ll be thankful that your Linux system already includes this Linux command by default.

The classic use for the yes command was to automatically answer “y” or “yes” to terminal prompts that ask questions for verification. For example, when you install packages via command line and the package manager asks “proceed with the installation?” and you have to give a response to continue. These days, many commands already have their own options that a user can specify to avoid the prompts that pop up, so yes isn’t as handy as it once was.

In Bash scripting and tasks that you need to automate, avoiding human interaction, yes can still prove very useful. It can also be used as a tool to quickly populate files with dummy text or spike your system’s CPU usage to 100% – in other words, the types of scenarios where you just need to test something.

In this guide, you’ll learn how to use the yes command in Linux through examples. Follow along below to see if this command will be useful in some of your Linux administration tasks.

In this tutorial you will learn:

  • How to use the yes command on Linux
yes command tutorial
yes command tutorial
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software yes
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The yes command will repeatedly output “y” or another string that we specify. Take a look at some of the examples below to see how the yes command in Linux actually works.

The yes command continues to repeat the output on the screen until interrupted
The yes command continues to repeat the output on the screen until interrupted

Yes command in Linux Basic and Advanced Examples

  1. Running the yes command by itself is not very useful. The command will simply output “y” as fast as it can – or rather, as fast as your CPU will allow it to.
    $ yes
    

    The only practical application this has is to quickly raise your CPU’s usage to 100%, which can be good for stress testing. The command will continue to run until you use a Ctrl + C keyboard combination to stop it.

    Running the yes command has spiked all of our CPU cores to 100%
    Running the yes command has spiked all of our CPU cores to 100%
  2. Let’s say that we have five files we need to delete. We can use the yes command to answer affirmatively when prompted about whether or not we really want to delete each file.
    $ yes | rm -i file*
    rm: remove regular empty file 'file1'? 
    rm: remove regular empty file 'file2'? 
    rm: remove regular empty file 'file3'? 
    rm: remove regular empty file 'file4'? 
    rm: remove regular empty file 'file5'?
    

    Full disclosure: this is not a very practical example, since the rm prompt can be easily bypassed without the need for yes. However, this example perfectly illustrates how the yes command is ideal for answering a bunch of prompts that pop up on our screen.

  3. By default, yes command in Linux will keep printing “y” characters. But you can use the command to print anything. If you needed to answer “no” to every prompt, you just need to pass that string to yes.
    $ yes no | rm -i file*
    
  4. You can even use it for bigger strings such as an entire sentence.
    $ yes this is just a random sentence!
    

    Using the yes command to repeatedly spam a random sentence
    Using the yes command to repeatedly spam a random sentence
  5. Remember that you can also send the output to any file you want. This might be good for creating a dummy file full of text, which can then be used for testing purposes in other scenarios.
    $ yes this is just a random sentence! > file.txt
    

    Be careful, because it won’t take long for this file to become huge. On our test system, the file was growing by 1 GB every few seconds. Remember to Ctrl + C to stop the command from running.

NOTE
You can always use the man command to read more about the yes command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts

In this guide, we learned all about the yes command on Linux. The greatest use for yes is to automate the answering of tons of terminal prompts. You may also find other niche scenarios where it comes in handy, such as some of the examples that we covered here.