Clear a Linux Terminal Without Losing Scrollback

Run plain clear and you might wipe scrollback you actually wanted to keep, because the default behaviour clears both the screen and the buffer. This guide shows the safe flag, and how to diagnose the two common failures: an unsuitable TERM value and a missing terminfo entry.

1. Check which clear will run

Run these read-only commands before putting clear in a script:

$ command -v clear
/home/linuxbrew/.linuxbrew/bin/clear
$ readlink -f "$(command -v clear)"
/home/linuxbrew/.linuxbrew/Cellar/ncurses/6.6/bin/clear
$ /usr/bin/clear -V
ncurses 6.4.20240113

The paths and versions are host-specific. The shell's command in the example is a Homebrew ncurses 6.6 build, while the packaged binary is ncurses 6.4. If you need the Ubuntu package described above, call /usr/bin/clear explicitly or fix your PATH deliberately. Do not infer the binary from the package name alone.

Checkpoint: use command -v clear and clear -V without clearing anything. If the version is not the one you intend, stop here and choose the exact path.

2. Clear the visible screen and keep scrollback

Use -x when you want to remove the current display but keep the terminal's scrollback history:

$ /usr/bin/clear -x

The command normally writes terminal control sequences rather than a message. The screen should redraw as empty, and scrolling up should still show earlier output. Use it when you want a clean working area without discarding context from a build, log, or troubleshooting session.

Checkpoint: scroll up after running the command. If your terminal does not retain scrollback, there is nothing for -x to preserve, but the command itself has still completed normally.

3. Clear the screen and scrollback together

With no -x, ncurses clear clears the screen and attempts to clear the scrollback buffer when the terminal advertises that capability:

$ /usr/bin/clear

This is a local display action, not a deletion from a file, shell history database, journal, or remote host. It does not alter the files that produced the output. It can nevertheless remove useful visual evidence from the terminal window.

Warning: treat this as irreversible from that window. There is no undo command for cleared scrollback. If you need the output, copy or save it before clearing.

Recovery: if you clear too early, rerun the command that generated the output, inspect the relevant log, or use your shell's history for the command itself. This will not restore output that was never written elsewhere.

4. Understand TERM and terminfo

clear reads the terminal type from TERM, then consults the terminfo database to select the right control sequences. Inspect the value without changing it:

$ printf 'TERM=%s\n' "$TERM"
TERM=xterm-256color
$ infocmp "$TERM" > /dev/null
$ printf 'terminfo status: %s\n' "$?"
terminfo status: 0

Your value may be different. A successful infocmp check means a matching entry was found; it does not describe the physical terminal perfectly. If TERM is empty or names an unavailable entry, clear can fail instead of guessing:

$ TERM=definitely-not-a-terminal /usr/bin/clear
'definitely-not-a-terminal': unknown terminal type.
$ printf 'exit status: %s\n' "$?"
exit status: 1

Do not solve this by copying a random TERM value from a different terminal. In a local terminal, let the emulator set it. Over SSH, check the client and server environment and ensure the server has the corresponding terminfo data. A bad value can affect cursor movement, colours, alternate screens, and other programs, not just clear.

5. Select a terminal type explicitly when testing

The -T option is useful for a controlled test or a wrapper that already knows the target terminal type:

$ /usr/bin/clear -T xterm-256color -x

When -T is present, the command ignores LINES and COLUMNS as well as the normal TERM selection. Use the exact terminfo name, and do not pass an untrusted value into a privileged script. If the name is unknown, the command exits non-zero and reports an unknown terminal type; install or select the correct terminfo entry through your normal system administration process.

-T does not change the terminal emulator's setting. It only chooses which instructions clear emits for this invocation. Remove it once the test is complete if ordinary environment-based selection is what you want.

6. Use it in scripts without hiding errors

For an interactive prompt, an alias is enough:

$ alias cls='/usr/bin/clear -x'

This alias lasts only for the current shell. Add it to a shell startup file only if you want that persistent behaviour, and remember that startup files affect every later shell. For a script, call the binary and preserve its status:

#!/bin/sh
if ! /usr/bin/clear -x; then
    printf '%s\n' 'Unable to clear: check TERM and terminfo.' >&2
    exit 1
fi

Do not redirect the command to a file expecting readable text. Its output is terminal control data, and redirecting it may save escape sequences rather than a useful transcript. Do not use sudo: clearing your terminal does not require root, and elevated execution can select a different environment and hide the real TERM problem.

Done means