HOWTO: fix garbled output in terminal after dumping a binary

Mar 28, 2016   #unix  #linux  #cli  #tty 

You tried to output a binary file, and now your terminal is garbled with nonsense. It is unreadable now! Been there, done that…

$ cat /boot/initrd.img-*

Yes! Boom! What was that and how to fix this?.. Turns out, there are many binary sequences that can harm the way your terminal prints things. Also, it is quite easy to fix:

  1. If you are running my favorite tmux press Ctrl-b x y (you will kill current pane)

  2. If you are running screen press Ctrl-a k y (you will kill current window)

  3. If this is just a bare terminal press Ctrl-z to send the task into background, then type reset and press Enter. Your terminal will become normal again. Now to kill that suspened task use:

    kill %1

    How to prevent this from happening again? Next time check file type in advance:

    $ file /boot/initrd.img-3.16.0-38-generic 
    /boot/initrd.img-3.16.0-38-generic: gzip compressed data, from Unix, last modified: Mon Mar 28 17:53:22 2016

Or use hexdump if you want to really see what is inside.