Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Kent W
Kent W
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Need comment for points my assisted/Error_reproduced comment.
Sorry to interrupt guys, but I have an actual solution to the OP's Q (next post)
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Gerwin Jansen
# Proceed related. No further comment from my side.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
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
@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.
@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.
@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?
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.
@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'"
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.
@gudii9 - Any of the suggestions that were given work for you? Appreciate any feedback ;)
Avatar of gudii9

ASKER

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?
Avatar of gudii9

ASKER

i like to close this question once i get more clarity. can you enable closing power to me back
@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.
Hello gudii9 - did you try my 2 suggestions here:
https://www.experts-exchange.com/questions/29084069/clear-command.html?anchorAnswerId=42525392#a42525392

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

2. alias cls="clear ; printf '\e[3J'"
(then use the cls command)
>> can you enable closing power to me back
What do you mean by this?