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
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
|# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
Yes command in Linux Basic and Advanced Examples
- 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.
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 + Ckeyboard combination to stop it.
- 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.
- 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*
- You can even use it for bigger strings such as an entire sentence.
$ yes this is just a random sentence!
- 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 + Cto stop the command from running.
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.
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.