What's the equivalent of the "cls" command from Windows/DOS? [duplicate]
Matthew Harrington
I used to use cmd back in windows, and the command line I used a lot, was cls. It's kind of like the clear command used in Linux, but it cleans the screen permanently.
If you use the clear command, it just scroll down, so that you don't see the command you where working on.
I like both a lot, but my question is, how do i get a cls like command, that clears the screen, and can't browse up, to see the command you where working on?
5 Answers
You can use reset. This resets the whole terminal, so that may be a bit overkill though.
Note on Konsole:
@Mechanical snail noticed "in Konsole 4.8.5, the old text is still there if you scroll up".@gertvdijk explained that it is "a feature. There's Ctrl+Shift+K for Konsole (reset and clear scrollback)."
Add this line to ~/.bashrc (i.e., the file called .bashrc, located in your home folder -- you can see it in Nautilus by pressing Ctrl+H):
alias cls='printf "\033c"'Now the cls command will clear the screen like in Windows. It will put you back to the top of a Terminal window, with no text shown above it. (It will not delete the shell's command history.)
This works because:
.bashrcruns every time abashshell starts up.- The
aliascommand defines theclscommand to run the command quoted on the right-hand side. printfcommand writes characters to the terminal. It accepts escape codes. The octal033is the character used to signal the beginning of a terminal control code. The control codectells the terminal to clear itself.- So, with this modification to
.bashrc. runningclssends the necessary data to the terminal to tell it to clear itself.
In your current terminal window, just type the following:
printf "\033c" If the aim is to avoid only casual rediscovery of command history, reset may be a viable choice.
However, bear in mind that by default, the shell logs your command history to a file as well - this is also eminently discoverable. If you want to prevent other persons from browsing your command history, you should also clean that out.
history -c # clear your history
cls works by clearing the buffer, which is fixed 25 or 50 lines in case of DOS and Windows, respectively. You can achieve something like this by writing so many lines in the buffer that it overflows (2000 is a typical value), but then reset is also a viable option as @lgarzo said.