Sometimes, while working on the command line, you arrive at a point where there's too much text on the terminal screen, and none of that is relevant to you. So, in order to avoid distraction, you'd want to clear the terminal screen. Those new to the Linux command line may not know that there exists a dedicated command line utility that does this work for you.
In this tutorial, we will be discussing the basics of clear (the tool in question) as well as how to use it. But before we do that, it's worth sharing that all examples/instructions mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
Linux clear command
The tool's man page makes it amply clear (no pun here) that the only use of the tool is to clear the terminal screen. Here's the exact definition from the man page:
Clear clears your screen if this is possible, including its scrollback buffer (if the extended "E3"
capability is defined). Clear looks in the environment for the terminal type and then in the
terminfo database to determine how to clear the screen. Clear ignores any command-line parameters
that may be present.
The following Q&A-type examples should give you a better idea of what clear does and how it works.
Q1. How to clear terminal screen using clear command?
To clear the terminal screen, following is the command that you need to run:
Yeah, that's it.
Q2. How clear command works?
If you take a detailed look after running the clear command, you'll find that it doesn't really clear the terminal. The tool just shifts the text upwards, out of the viewable area (pressing Ctrl+L also does this).
So what you should know is that the clear command works by producing console escape codes in the output. The type of codes produced depends on the type of terminal being used. For more information on these codes, head here.
Q3. How to really clear terminal screen?
So now you know how the cleat command works, and want to really clear the terminal screen. Well, that's possible and isn't difficult either. Just run the following command:
The reset command re-initializes the terminal. While the tool does what it claims, it's usually a bit slow to execute. So, here're some other alternatives:
For more information on how the above commands clear the terminal screen, head here. This forum link also contains other useful information like settings you need to do in PuTTY for the aforementioned commands to work. So do go through the link carefully.
The clear command, as you'd agree, is very easy to use. It's basic usage is what you should know (needless to say, there's practically no learning curve in this) as more often than not, you'll be happy with what the tool does by default. However, we've also mentioned some details on how it works and what are some of its alternatives - you may find this information useful in some scenarios.