- Unix Commands Reference
- Unix Commands - Home
clear Command in Linux
clear is a Linux command that is used to clear the terminal screen. When you are working in a terminal, the commands you have executed and their outputs can quickly pile up. This makes the terminal look messy and difficult to read.
Executing the clear command will remove all the messy contents from the terminal, including the scrollback buffer and give your terminal a fresh start. No additional options are required with the command, you just simply type clear, press Enter to enjoy a clean empty terminal screen.
Table of Contents
Here is a comprehensive guide to the options available with the clear command −
- How to Install clear Command in Linux?
- Syntax for clear Command in Linux
- Different Options Available for clear Command
- Examples of clear Command in Linux
- Alternates of clear Command in Linux
How to Install clear Command in Linux?
The clear command is included in the package named ncurses-bin that comes preinstalled on most Linux distributions. However, if the package is accidentally removed or unavailable on your system, you can install using your default package manager.
For Linux systems like Ubuntu, Debian and other such systems, the ncurses-bin package can be installed from the apt command provided below −
sudo apt install ncurses-bin
For RedHat based Linux distributions like Fedora and CentOS, you can install ncurses-bin package on the system from the below-given command −
sudo yum install ncurses-bin
To confirm ncurses-bin is successfully installed on you desired Linux system, simply run the below-given command:
clear -V
You can also use the which command followed the keyword clear to confirm the clear command installation in Linux −
which clear
Syntax for clear Command in Linux
The typical syntax to use the clear command in Linux is provided below −
clear [options]
Different Options Available for clear Command
The clear command is mostly used without an argument, however, there are a few options that you can use with the command to alter its behavior. The options that you can use with the clear command are provided in the table below −
Option | Description |
---|---|
-T type | Specifies the terminal type to use. For example, -T xterm or -T vt100 |
-V | Displays the version information for the clear command. |
-x | Prevents clear command from attempting to clear the scrollback buffer. |
Examples of clear Command in Linux
Let’s discuss some basic examples of clear command in Linux system −
Basic clear Command
The basic clear command is the one that is used without any argument. When you use a clear command without an option, it will simply wipe the terminal screen, providing a fresh start. It erases all visible content, making the terminal clean and a quick way to tidy up your terminal and make it more readable.
clear
Clear with -x Option
When you use the clear command with the -x option, it prevents the command from attempting to clear the scrollback buffer. In simple words, it won’t affect the history of commands and outputs stored in the scrollback buffer.
It is pretty useful in case you want to keep the command history intact while still clearing the visible screen. Further, it may come handy for remote sessions or when working with scrollable terminals.
clear -x
Alternatives of clear Command in Linux
As previously discussed the function of the clear command is to clear the terminal screen and give your terminal a fresh start. Like clear command, there are some other alternatives that you can use to perform the similar operation, these are provided below −
1. Use Shortcut Key
A quick way to clear your terminal screen is to use the keyboard shortcut key CTRL+L.
2. Use reset Command
You can also use the reset command instead of clear to clear your terminal screen. The advantage of using reset command is that it also resets the terminal’s settings to their default values.
reset
3. Use Escape Sequence
Some terminals recognize the escape sequence like printf "\033c", printf "\ec", printf"\x1Bc" and immediately clear your terminal screen. You can use any of these sequences to confirm whether this will clear your terminal screen.
4. Creating an Alias
You can also create a shortcut for clearing your terminal screen using the alias command. For that purpose, you can simply follow the below-given syntax and add your desired command for clearing the screen.
alias c='add_command'
Replace add_command with the command of your choice like you can use clear, reset, or escape sequence.
Conclusion
The clear is a powerful Linux command that helps you clear your terminal screen. While it is commonly used without any arguments, you can also customize its behavior using options like -T type or -x. You must keep your terminal tidy as it will enhance its productivity and readability.
Whether you are a seasoned developer or just starting out, mastering the essential commands like clear is essential for a smooth command-line experience.