The clear
command in Linux is used to clean up the terminal screen by removing all previous commands and output, giving you a fresh, empty workspace. It doesn’t close the terminal or end any running processes—it simply clears the visible clutter, making it easier to focus on new tasks. The command works by sending special control sequences to the terminal, which instruct it to reset the display.
While clear
is the most common way to achieve this, there are other methods, such as keyboard shortcuts (Ctrl + L
) or escape sequences, that can also clear the screen. Whether you’re working in a standard terminal, a script, or a terminal multiplexer like tmux
or screen
, knowing how to clear the screen efficiently is a fundamental skill for Linux users.
clear Command
The most common command to clear the terminal screen.
clear
- Clears the screen but retains scrollback history (you can scroll up to see previous output).
Ctrl + L Shortcut
A keyboard shortcut to clear the terminal screen.
Ctrl + L
- Works the same as the
clear
command but is faster and doesn’t require typing.
reset Command
Resets the terminal to its default state, clearing the screen and fixing any display issues.
reset
- Useful if the terminal becomes unreadable due to binary data or corrupted output.
printf Command
Clears the screen by printing escape sequences.
printf "\033c"
- Sends the ANSI escape sequence
\033c
to reset the terminal.
tput reset Command
Clears the screen and resets the terminal using tput
.
tput reset
- A more robust way to clear the screen and fix terminal issues.
echo Command with Escape Sequence
Clears the screen using an escape sequence.
echo -e "\033c"
- Similar to
printf
, but usesecho
with the-e
flag to interpret escape sequences.
alias for Quick Clearing
Create a custom alias for clearing the screen.
alias cls='clear'
- Now, you can simply type
cls
to clear the screen.
clear with Scrollback Clearing
Clear the screen and remove scrollback history.
clear && printf '\e[3J'
- Clears the screen and deletes the scrollback buffer (useful for sensitive data).
clear with ANSI Escape Codes
Clear the screen using ANSI escape codes.
echo -e "\033[H\033[2J"
- Moves the cursor to the top-left corner (
\033[H
) and clears the screen (\033[2J
).
clear in Scripts
Use clear
in scripts to start with a clean screen.
#!/bin/bash
clear
echo "Starting the script..."
clear with watch Command
Clear the screen before running a command repeatedly.
watch -n 1 "clear; ls -l"
- Clears the screen every second before listing files.
clear with tmux or screen
Clear the screen in terminal multiplexers.
Ctrl + A, then C # In screen
Ctrl + B, then C # In tmux
- Clears the screen within
tmux
orscreen
sessions.
clear with stty Command
Reset terminal settings and clear the screen.
stty sane
clear
- Fixes terminal issues and clears the screen.
clear with script Command
Clear the screen while recording terminal activity.
script -c clear output.txt
- Clears the screen and starts recording terminal output to
output.txt
.
clear with history Command
Clear the screen and command history.
clear
history -c
- Clears the screen and deletes the command history.
Bonus: Create a Permanent Alias
Add a custom alias to your ~/.bashrc
or ~/.zshrc
for quick clearing:
alias cls='clear && printf "\e[3J"'
- Reload the shell configuration:
source ~/.bashrc
- Now, use
cls
to clear the screen and scrollback.
Key Notes
- Use
clear
orCtrl + L
for quick screen clearing. - Use
reset
ortput reset
to fix terminal display issues. - For sensitive data, clear the scrollback buffer with
printf '\e[3J'
.
These commands and shortcuts will help you keep your terminal clean and organized! 🐧