Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

When working with the Linux command line, it’s a very common scenario that the terminal gets filled up with the commands and their output. Clearing the terminal to declutter the screen can help to improve our productivity.

In this tutorial, we’ll discuss the various ways to achieve this.

2. Using the clear Command

One common way to clear the terminal screen is to use the clear command. Let’s fill up the terminal with few commands and then execute the clear command:

$ pwd
/home/groot
$ hostname
workstation
$ clear

Note that the clear command also clears the terminal scrollback buffer if the extended E3 capability is defined.

To prevent clearing the scrollback buffer, we can use the -x option:

$ clear -x

3. Using the reset Command

One more way to clear the terminal screen is to use the reset command. Let’s execute few commands and clear the screen using the reset command:

$ pwd
/home/groot
$ hostname
workstation
$ reset

The reset command reinitializes the terminal. It’s useful when the program doesn’t terminate gracefully, leaving our terminal in an abnormal state.

4. Using the Console Codes

We can use the “ESC c” console code to clear the terminal screen. The ASCII value of the escape character is decimal 27. We can represent the same number in octal and hexadecimal format as 033 and 0x1B, respectively.

Let’s execute some commands to fill up the terminal and clear it using the octal console code:

$ pwd
/home/groot
$ hostname
workstation
$ printf "\033c"

We can achieve the same result using the console code in hexadecimal format:

$ printf "\x1Bc"

5. Conclusion

In this tutorial, we discussed various examples to clear the terminal screen. We can use these commands in day-to-day life while working with the Linux system.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!