173

I'd tried command cat with an executable file:

cat /bin/ls

Now I can't read any word in this terminal (Linux console). How can I fix it?

3

9 Answers 9

245

Often times when in a Unix/Linux terminal (Bash) for example you'll use the commands more or less or cat to view a file. When you do this and the file isn't meant to be viewed (such as /bin/ls) you'll get output like this:

                ss of binary output

What's going on here is that you just tried to view a file that's a program. An executable which aren't meant to be viewed with standard viewers as I mentioned above.

method #1 - reset

To fix this issue you can do the following:

  1. Hit Control + C a couple of times (Ctrl+C)
  2. Type the command reset and hit return

This should usually put your terminal back into a more normal mode. I'll mention one more thing, when you do the steps above, you'll by typing them blind into your terminal. So just make sure you're typing it correctly.

method #2 - stty sane

As suggested in the comments by @sendmoreinfo you might have better luck using the following commands instead if the above doesn't work:

$ stty sane
$ tput rs1

determining a files' type

Incidentally, if you come across a file and aren't sure if it's going to mess up your terminal you can inspect the file using the command file which will report back the type of file it is.

For example, with /bin/ls that file shows the following output:

$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped
7
  • 29
    reset is not a 100% cure for terminal madness. Try stty sane followed by tput rs1. Jun 17, 2013 at 14:22
  • 1
    @sendmoreinfo - thanks I've added your suggestion to the answer. I'd used stty sane but never tput.
    – slm
    Jun 17, 2013 at 15:01
  • 1
    actually, I need to research this more. reset is supposed to send rs1 string. A good test (for xterm, at least) is to make cursor invisible and see if reset brings it back. Jun 17, 2013 at 17:15
  • 4
    reset worked in my case
    – code_monk
    Mar 8, 2015 at 14:06
  • 2
    clear also works for mild cases.
    – Navin
    Nov 2, 2015 at 5:41
56

I've had an occasion where none of the usual tricks, reset or stty sane, worked (after accidentally calling print on a python bytearray). I had success with method 2 listed on this helpful blog.

I've since created a most helpful alias:

alias fix='echo -e "\033c"'
8
  • 3
    I know this is old, but this is the only answer above that worked for me! Thanks
    – jredd
    Jun 1, 2016 at 18:31
  • This echo -e "\033c" helped me to clean up a character-garbled SSH login terminal session that was mixed up after a cat /etc/localtime.
    – Pro Backup
    Dec 20, 2016 at 21:17
  • This worked with OpenBSD, when all others did not.
    – DHW
    Apr 19, 2017 at 5:38
  • 1
    further experimentation with screen(1) found force detatching of all sessions (-D) then issuing the reset commands to the underlying terminal screen was run in, could fix the issue. may also need to rename corrupted window names that may re-display the same binary data at the terminal causing it to corrupt (and then reset yet again).
    – math
    Oct 18, 2017 at 4:41
  • 1
    My terminal size ( LINES ) was cut in roughly half after using vi while connecting to the console of a remote linux host via SOL. The remote session had LINES=24 whereas my local MAC session had LINES=51. echo -e "\033c" worked for me but none of the other fixes I tried did ( 'stty sane', tput rs1 , tput sgr0, reset ).
    – Mark
    Dec 19, 2017 at 19:33
18

No single prior answer worked for me. But this seemed to do the trick in .bashrc add:

alias fix='reset; stty sane; tput rs1; clear; echo -e "\033c"'

and then when the problem occurs type this (even though you probably can't see it!)

(ctl-c, ctl-c, ctl-c)
fix

Much thanks to the prior submitters. Also, as a side note, the reason that your terminal goes wonky when showing those executable files (or keystores, etc) is that those files often contain binary sequences that are control codes. The control codes may do random things like switch to a graphics character set, set foreground and background colors to the same thing, etc.

2
  • Same happened. Nothing worked for me, just this combo...
    – TrueY
    Feb 16, 2018 at 16:56
  • 1
    I called my sane
    – qodeninja
    Mar 16, 2019 at 5:25
6

I have almost the same alias as the previous post, with one small change (tput reset instead of rs1) and one added command (setterm -reset):

alias clr='echo -e "\033c" ; stty sane; setterm -reset; reset; tput reset; clear'
5

tmux users might be able to send all these commands to their shell, but the resetting doesn't get elevated to the tmux pane.

Make new tmux window: ctrl-B c tmux list-panes Note the pane number you think is borked, typically 0. Let's call it PPP

Select the tmux window, where XXX is the window number which is borked, not necessarily the same as pane number. ctrl-B XXX tmux send-key -R -t PPP

You will then see all the commands in the other answers to this question which made it to your shell, but didn't work! Maybe a blind tmux send-key -R -t PPP will work, but I can't test.

5

I know this has been here a while, but while many of the other answers have worked for me, reset, stty sane, sometimes neither of those has worked for me and I always resort to:

echo ^v^o

That's echo Ctrl+V followed by Ctrl+O

1
  • this does the trick for me on a remote SSH session using the bitvise terminal Dec 8, 2023 at 21:24
3

Nothing above helped me. However, Jack Wasey mentioned tmux, so I just ran tmux, exited again and everything was back to normal.

Another solution is:

reset
1
  • Only running tmux and exiting worked for me. No other thing mentioned on this page. (My exact problem was: under WSL in default terminal window (not Terminal!) mouse commands were generating escape sequences that were not interpreted, i.e.: 2;53;18M2;53;18m.)
    – Zbyl
    Sep 28, 2021 at 10:25
1

In addition to other answers advising how to reset the terminal, I figured the corruption cannot be avoided unless properly guarded. So I better send the output to a simple text-friendly converter cat -v:

docker logs myjenkinscontainer 2>&1 | cat -v

Viewing and editing binary files seems possible with hexdump -C and vi -R (:%!xxd -g1 for a hex view and :%!xxd -g1 -r for saving hex changes).

1
  • It took a while to learn that termcap/terminfo's are for applications. That is, they are terminals' "user manuals" and do not protect the terminal against their interpreting escape and control characters sent by applications that disregard visualization concerns. Luckily there is a layer in between, tmux, along with its option to avoid translating for the underlying terminal (and drop!) the special characters sent by applications to its virtual screen driver, set-option -ga terminal-overrides ',*:enacs@:smacs@:rmacs@:acsc@'. The option works in recent versions of tmux such as 2.3.
    – eel ghEEz
    Dec 24, 2020 at 3:15
0

This garbled terminal mode is called Alternate Character Set , ACS for short.

to get out of it:

$ tput sgr0             # stop all special effects, including ACS

$ setterm --default     # equivalent
$ setterm --reverse=off # equivalent

or

$ tput rmacs       # stop ACS

to start ACS:

$ tput smacs       # start  ACS

on some terminals you need to not only start it but also enable it:

$ tput enacs smacs   # enable and start ACS, only some terminals require this

if your terminal also has other defects like colors, background, bold, italic, blink, ... :

$ tput sgr0        # disable all special effects, including ACS

# you can experiment with these control sequences:
$ tput bold       # bold/bright
$ tput rev        # reverse mode
$ tput setaf 1    # set foreground color to red [0-7]
$ tput setab 3    # set background color to yellow [0-7]
$ tput bold setaf 1 setab 3    # combined sequence

recommended : if you want to never have to worry about these defects in your working environment, you can integrate the reset sequence into you prompt, at the appropriate place in your init script/config like this:

# in init script , for example ~/.bash_profile
export PS1='\['`tput sgr0`'\] \u@\h \w \$ '

# or prompt with colors and reset at the end
export PS1=' \[\e[37m\]\u\[\e[m\]@\[\e[32m\]\h \[\e[37m\]\W \$ \['`tput sgr0`

other commands that could influence it:

# the following two enable/disable ACS, the effects are reversed on some terminals
# these sometimes interact with tput smacs/rmacs in wierd ways
$ echo ^V^O    # echo Ctrl-V,Ctrl-O,Enter
$ echo ^V^N

more ways to stop ACS, also clears the screen:

$ echo -e '\ec'                  #
$ echo -e '\033c'                # these 3 are equivalent
$ echo <ctrl-v><esc><c>          #

$ reset                 #
$ tput rs1              # rs2/rs3
$ tput reset            #          don't work on some terminals
$ setterm --reset       #
$ setterm --initialize  #

if you want to inspect the control sequences, which are different on different terminals:

$ tput sgr0 | cat -v
$ tput sgr0 | od -c    # or od -ca for more representations 
$ infocmp

more info in the man pages for terminfo, setterm, infocmp, tput, cat, od

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .