Let's say one creates a file like so:

touch myFile

You enter some text in it with vim or whatever, and then use cat myFile to spit the contents out into the terminal.

Now, what happens when I use cat on any image? Say,

cat myPNG.png

I just get a bunch of garbage. It just made me think about what the cat command is attempting to do, and where all of this "garbage" comes from. Just curious.

2 Answers 2


It may be useful to explain how files work at the lowest level:

A file is a stream of bytes, zero or more in length. A byte is 8 bits. Since there are 256 combinations of 8 bits, that means a byte is any number from 0 to 255. So every file is, at its lowest level, a big hunk of numbers ranging from 0 to 255.

It is completely up to programs and users to decide what the numbers "mean." If we want to store text, then it's probably a good idea to use the numbers as code, where each number is assigned a letter. That's what ASCII and Unicode do. If we want to display text, then it's probably a good idea to build a device or write a program that can take these numbers and display a bitmap looking like the corresponding ASCII/Unicode code. That's what terminals and terminal emulators do.

Of course, for graphics, we probably want the numbers to represent pixels and their colors. Then we'll need a program that goes through the file, reads all the bytes, and renders the picture accordingly. A terminal emulator is expecting the bytes to be ASCII/Unicode numbers and is going to behave differently, for the same chunk of bytes (or file).

  • Excellent explanation! Glad you went down to the lowest level.
    – Qcom
    Apr 26, 2011 at 3:01
  • 2
    @BOSS I'd throw in the definition of a bit. A bit is a Binary DIgit or a number that can be a one or a zero. One bit has two possibilities or 2^1=2. Two bits can have four combinations or 2×2=4. More correctly, it's 2²=4 for 2 digits. 8 Binary DIgits or bits can have 2^8=2×2×2×2×2×2×2×2=256 possible combinations.
    – penguin359
    Apr 26, 2011 at 5:45
  • 2
    Minor nitpick/pedantry: a byte is of an arbitrary size. You may come across situations in which it is not 8 bits wide. "Octet" is the preferred terminology.
    – Chris Down
    Nov 2, 2012 at 22:21

Cat dumps the contents of the input file to standard output, which in this case is a text device: your terminal window. Since an image consists of binary data, what you are seeing is the raw binary data interpreted as though it were ASCII text. For example, a byte with the value 65 would be displayed as upper case A, 66 is upper case B, etc. If you open the file with an application capable of interpreting the image file correctly, the binary contents of the file will be displayed as an image.

  • OK, cool. Thanks for the answer. You mention a "text mode". Are there other modes for a standard terminal? Or, is the only other way to interpret it like you said, via an application that can properly render that filetype?
    – Qcom
    Apr 26, 2011 at 2:34
  • 4
    @BOSS, no, I believe gordoco meant to say that a terminal is a text device. The original purpose of cat is to put files together as in cat filea fileb > filec, if you don't transit via the terminal (as in this example) then it does not matter whether the file if text or binary. In the unix world, binary and text files are stored the same, only text files contain a somewhat restricted set of bytes. A common use of cat is to direct file contents to commands that do not take file names as parameters, but accept input from the standard input.
    – asoundmove
    Apr 26, 2011 at 3:25
  • @BOSS, saying a file is a text file as opposed to binary, only means that the file's binary content can be interpreted as text. Things used to be simple, either the file was ASCII or it was not. Nowadays the file could be a number of different text formats, including ASCII, UTF8, UTF16... On the hard drive they are all binary.
    – asoundmove
    Apr 26, 2011 at 3:30
  • @asoundmove: You are correct, I should have said text device, rather than the ambiguous term "text mode". I've edited my response.
    – juggler
    Apr 27, 2011 at 13:32
  • I used cat on a bitmap just now and it printed to terminal not just ascii symbols, but also unicode characters (like "ڣ" and "漥"). Any idea why this is?
    – Magnus
    Jan 2, 2020 at 1:37

You must log in to answer this question.

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