clear command

Does not clear command clears whole screen.

what i noticed is it still kept lot of stuff at the top like previous files, commands i gave etc.

Please advise how to clear everything on clear and do fresh
grep xyz *
afterwards to see only above grep results?
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent WSr. Network / Systems AdminCommented:
The clear command acts differently depending on your terminal emulator (your ssh client, most likely). In most, it just clears the immediately displayed lines (the current visible portion). It sounds like you are asking why it does not clear the buffered text above, when you scroll up?
 
If this is the question, then that would be more of a terminal emulator buffer setting. The buffer does not come from the linux machine you are issuing the command on, but are rather a capture in your terminal emulator itself.  In other words, the clear command clears your screen, but it can not clear your local ssh clients buffer.  The only way to really achieve that is to manually clear your clients buffer, and if you want to retain this behavior, either disable buffering all together or set it to not buffer more than your usual screen dimensions line count.

By contrast, if you are logged in on a natural tty directly connected to the box, the clear command would work as you expect it to here, because there is no 3rd party terminal client capturing your input and committing it to a clients buffer feature.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic Advisor Commented:
>> Does not clear command clears whole screen.
Sure it does. It may not for you but to help you further you must tell us your relevant system details.

DESCRIPTION
       clear  clears  your screen if this is possible.  It looks in the environment for the terminal type and then in the terminfo database to
       figure out how to clear the screen.

       clear ignores any command-line parameters that may be present.

You can always try the reset command instead.
1
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@gudii9
It happens using invalid versions of /usr/bin/clear.
a) working case:
$ for ValidClear in {1..28};do echo $ValidClear;done
$ /usr/bin/clear

Open in new window

b) Reproducing clear error (All lines above for loop are cleared):
$ for ValidClear in {1..30};do echo $ValidClear;done
$ /usr/bin/clear

Open in new window

Here goes the way to handle such situation:
a)
$ alias c='echo -ne "\033[H\033[2J"'
$ alias clear='echo -ne "\033[H\033[2J"'
$ c
$ clear
$ /usr/bin/sudo /bin/mv -i /usr/bin/clear /usr/bin/clear.NotWorking

Open in new window

b)
i) Open putty.exe
ii) Click Load
In left pane, drill down
+Window
        Lines of scrollback
Use the number 900000000
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Here goes actual error behind that:
/usr/bin/clear.NotWorking using characters command (written echo -ne to print):
               echo -ne "\033[3;J\033[H\033[2J"
Related output from /usr/bin/clear.NotWorking:
$ /usr/bin/clear.NotWorking | /usr/bin/od -bc
0000000 033 133 063 073 112 033 133 110 033 133 062 112
        033   [   3   ;   J 033   [   H 033   [   2   J
0000014

Open in new window

Actually this needs to be:
               echo -ne "\033[H\033[2J"
Hence handled that using:
$ echo "#include <stdio.h>
int main()
{
        printf( \"\033[H\033[2J\");
        return 0;
}" > murugesandinsClear.c
$ $ /usr/bin/sudo /usr/bin/gcc -Wall -O murugesandinsClear.c -o /usr/bin/clear
[sudo] password for murugesandins:
$ # Working case
$ /usr/bin/find ~/
ALL OUTPUTS HERE
...
...
TO INCREASE MORE OUTPUT LINES.
$ /usr/bin/clear
$ # Previous find output was not cleared and /usr/bin/clear is working now.
$ alias c='/usr/bin/clear'

Open in new window

\033 => pointing to Ctrl v ctrl [
Example:
$ echo -ne "^[" | /usr/bin/od -bc
0000000 033
        033
0000001

Open in new window

Output obtained using man echo:

                 -n     do not output the trailing newline
                 -e     enable interpretation of backslash escapes
$ export PS1='$ '
$ echo Output having new line
Output having new line
$ echo -n Output without new line
Output without new line$ /bin/pwd
/home/murugesandins
$ 

Open in new window

0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
Need comment for points my assisted/Error_reproduced comment.
0
Duncan RoeSoftware DeveloperCommented:
Sorry to interrupt guys, but I have an actual solution to the OP's Q (next post)
0
Duncan RoeSoftware DeveloperCommented:
What the Original Poster  wanted was a way to clear the screen buffer in his terminal emulator (xtrerm, rxvt Terminal etc.).
Let's say it's an xterm, although the following works for any emulator.
There is no escape sequence to clear the terminal buffer. The only way to get a screen with an empty buffer is to exec a fresh copy of the emulator. At least for xterm, command history is preserved when you do this
exec $(cat /proc/$(echo $(ps -l|grep -w $(echo $0|sed 's/-//'))|awk '{print $5'})/cmdline|xargs -0 -n1 printf "%s ")&sleep 1;exit

Open in new window

When I execute this command, the screen clears and I have no buffer. For others, the new window may appear elsewhere on the screen (I start my xterms in fixed positions). And, if you resized the xterm after starting it, the new one will be the old size.
The sleep 1 is a little messy, but you need it (unless you want to keep the old xterm).
Next post shows how this complex-looking command works.
0
Duncan RoeSoftware DeveloperCommented:
One simplification: you don't need the exec at the front
$(cat /proc/$(echo $(ps -l|grep -w $(echo $0|sed 's/-//'))|awk '{print $5'})/cmdline|xargs -0 -n1 printf "%s ")&sleep 1;exit

Open in new window

Overview: the xterm to replace will be the parent process of the current command shell. Start a new instance of that xterm (or other emulator) with the same command that started the current instance. Run this in a separate process so we retain control of the current xterm. Wait for the new xterm to get started (no easy way to do this so use sleep). Exit the current shell, thus killing the current xterm.
From the inside outwards:
echo $0 Name of the current shell.
sed 's/-//' Current shell name will start with a hyphen if a login shell, so get rid of it.
$(echo $0|sed 's/-//') simple shell name, e.g. bash.

Sorry, have to go now. Will do the rest later.
0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@Gerwin Jansen
# Proceed related. No further comment from my side.
0
Duncan RoeSoftware DeveloperCommented:
Sorry - please ignore my previous posts. There is an Escape sequence that will clear the screen buffer
/bin/echo -e '\ec'

Open in new window

This sends Escape c to the terminal which  is a Full Reset (RIS) according to the X Consortium document Xterm Control Sequences (xterm.seq on my system).
This sequence clears the screen and empties the scroll buffer. I have tested it on xterm and rxvt.
(This behaviour differs from the xterm control-Button-2 (VT Options) Do Full Reset menu item, which doesn't clear the scroll buffer).
0
Duncan RoeSoftware DeveloperCommented:
There was no good answer until yesterday. https:#a42522669 nails it. It answers the Q succinctly and correctly.  The author may respond, but it is a long time since he posted the Q.
I am concerned that I never got an NQA (Neglected Question Alert) for this Q. I get NQAs for loads of stuff I know nothing about, but no longer, it seems, for the so-called Linux OS Dev TA (formerly Linux Programming). I just happened to look at that TA and found this Q (among others).
Even if the author doesn't respond, the answer may help others (it was a revelation to me).
0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
I was able to:
1. reproduce
2. Made backup for clear command
3. reimplemented using C program or echo command or alias.

>> @Gerwin Jansen
>> # Proceed related. No further comment from my side.
>> please post your suggestion
0
Gerwin Jansen, EE MVETopic Advisor Commented:
@duncan roe - I know this user used PuTTY (no xterm or rxvt) - does your proposed solution work in PuTTY?

@ Murugesan Nagarajan - Your first 2 'academic' responses were not valued by other experts, that's why I didn't assign (assisted) points. Meanwhile you've asked several times for points - that is not what you should do.

Another possible solution: since PuTTY is used - you can use "Restart Session" from the menu. Note that this does also reset the current working directory etc.
0
Duncan RoeSoftware DeveloperCommented:
@Gerwin Jansen - I have never used PuTTY so I don't know if it would work. If PuTTY is some kind of graphical application on a Windows system, I fear it might not.
But, the fact that he uses the clear command suggests to me some kind of xterm-ish thing. The escape sequence I posted corresponds to the more general tput rs1 terminfo command, which may work for putty.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
@Duncan Roe - PuTTY is "PuTTY is a client program for the SSH, Telnet and Rlogin network protocols.". It's used on Windows machines to connect to Unix/Linux servers, typically through ssh.

I've tested a bit and found out that your "/bin/echo -e '\ec'" does not work, I ran find / and then the echo command, I could scroll back through previous history.

I tested 'clear' as well, that works partially: after the first clear, history is gone but for 1 screen, if you run clear again, it clears that last screen as well.

Then I tested the echo -ne "\033[H\033[2J" command by Murugesan Nagarajan - it does not work in PuTTY.

And finally I tested 'reset' which does not work for my home Linux box (worked @work machine) - so I would rule that out for now as well :(

So what I see now is that "clear ; clear" is working as requested by the asker.

Confirmation of other experts?
0
Duncan RoeSoftware DeveloperCommented:
Did you also try tput rs1?
clear;clear does not have the desired effect in an xterm (nor in a CMD window which does not recognise clear as a command): this may be a putty thing.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
@Duncan Roe - I tried tput rs1 - it does not work in PuTTY - I have no means of trying xterm (terminal in MacOS doesn't work either) - what is your setup exactly how you tested with xterm?

Duh, PuTTY has a 'Clear Scrollback' option in the menu - does exactly that.

Something else I tried is this:  printf '\e[3J'  - it clears the scrollback buffer, can you try that?

Combined with clear and setting an alias is working for me in PuTTY: alias cls="clear ; printf '\e[3J'"
0
Duncan RoeSoftware DeveloperCommented:
Yes, printf '\e[3J' does clear the xterm scrollbar.
Interestingly, this is undocumented: xterm.seq only documents Ps = 0, 1 or 2 for ESC [ Ps J, and the sequence is not in xterm's terminfo entry.
I have a Linux (fvwm) desktop. I saw this Q in the Linux OS Dev topic area, so naturally answered it as a Linux question.
1
Gerwin Jansen, EE MVETopic Advisor Commented:
@gudii9 - Any of the suggestions that were given work for you? Appreciate any feedback ;)
0
gudii9Author Commented:
sorry experts for late response
reset did not work

obviously
clear never worked

other solutions seemed too long for me to understand.

i am still at same place where i was.

is there is any short sweet command which does magic?
0
gudii9Author Commented:
i like to close this question once i get more clarity. can you enable closing power to me back
0
murugesandinsC C++ Shell script AIX CYGWIN_NT HP-UX Linux MINGW SunOSCommented:
@gudii9
$ /usr/bin/clear.NotWorking | /usr/bin/od -bc
0000000 033 133 063 073 112 033 133 110 033 133 062 112
        033   [   3   ;   J 033   [   H 033   [   2   J
0000014

Open in new window

will display the character being used to clear the screen.
This has been achieved by piping the output to "/usr/bin/od -bc"

Using the similar command at your system
/usr/bin/clear | /usr/bin/od -bc

Open in new window

you can know which characters are being used to clear the screen.
Try using similar characters using echo command
echo -ne "\033[H\033[2J"

Open in new window

so that you can understand what clear is doing to clear the screen.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Hello gudii9 - did you try my 2 suggestions here:
https://www.experts-exchange.com/questions/29084069/clear-command.html#a42525392

1. PuTTY has a 'Clear Scrollback' option in the menu

2. alias cls="clear ; printf '\e[3J'"
(then use the cls command)
0
Gerwin Jansen, EE MVETopic Advisor Commented:
>> can you enable closing power to me back
What do you mean by this?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.