Produced after using cat on an image

binarycatfiles

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.

Best Answer

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).

Related Question