3

What does the output mean when you do cat image.png?

Is this what the image actually is made of?

Does it matter what file extension the image has?

  • Have you actually tried that command? – 9ilsdx 9rvj 0lo Jun 21 '17 at 11:10
  • Yes, I actually have. – NerdOfCode Jun 21 '17 at 13:47
  • Don't cat binary files. If your terminal is in any way set up to interpret control characters, this causes undefined behaviour - from deleting your screen to messing up your terminal characteristics, etc. It can even cause arbitrary commands to be executed in some circumstances. As you probably know, this is really dangerous if someone were to craft a malicious file and trick you into catting it. – Kilian Foth Dec 5 '17 at 8:51
  • Oh ok... I never really shared a system so I never thought about it... – NerdOfCode Dec 5 '17 at 12:52
11

cat will print out the content of the file (zeroes and ones which will be mapped to meaningless characters, because they're not really characters right?) into stdout which by default is your terminal.

The file extension is for us (humans), to easily figure out what is the type of file; some programs use it to open it too but mostly programs detect the file type using a header inside the file.

Use file file.png to get the file type, or xxd file.png:

00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR
00000010: 0000 0002 0000 0002 0800 0000 0057 dd52  .............W.R
00000020: f800 0000 0e49 4441 5478 9c63 6c60 6062  .....IDATx.cl``b
00000030: 6000 0002 9200 84f7 7ca3 5800 0000 0049  `.......|.X....I
00000040: 454e 44ae 4260 82                        END.B`.

to get a hex dump of the file content, notice the "PNG" in the first line.

Also from this file signatures reference we can see that PNG signature is:

PNG     89 50 4E 47 0D 0A 1A 0A     PNG image
9

cat simply outputs contents of the file to stdout stream, which in your case happens to be terminal. What happens with the sequence of 0's and 1's that comprise the file is up to the terminal. In case of binary file, sequences of bits that aren't text won't be recognized as text.

Consider for example cating a file that has UTF characters, such as Chinese in two different terminals - one that has support for UTF characters (terminator in the screenshot below) and one that doesn't have a good support (xterm in the screenshot below). As you can see some characters aren't interpreted. cat will output contents in either case, but how to interpret those contents - it's up to each terminal.

enter image description here

Now, if you redirect stdout stream to a file, you're effectively creating a copy, pretty much the same as cp original copy or mv old new. In some rare cases you might want to splice two files - binary or text - which is also possible with cat. The idea is the same. In accordance with POSIX definition, "[t]he input files can be any file type", which means cat can read any file. However, there are some cases where cat will assume text mode. In case of Ubuntu's cat ( which is provided by GNU coreutils package) information provided in info cat states:

‘cat’ reads in text mode if one of the options ‘-bensAE’ is used or if ‘cat’ is reading from standard input and standard input is a terminal. Similarly, ‘cat’ writes in text mode if one of the options ‘-bensAE’ is used or if standard output is a terminal.

Of course, if we actually want to view contents of binary file in readable format, we can use hex editors or any "dump" type of program, Midnight Commander file manager, or even vi text editor. There's a pretty good list of those here and here already.

  • 4
    cat doesn't try to interpret anything. It just reads the sequence of files given in command line and writes them to stdout one after another. – Ruslan Jun 21 '17 at 10:19
  • @Ruslan I've edited the answer. All corrected now, plus included a few extra bits here and there. – Sergiy Kolodyazhnyy Nov 26 '17 at 11:38
3

Images are made of bytes. So are text files

When a software application, for example cat wants to read text files, it starts reading bytes and searches those bytes in character encodings such as ascii or unicode. It finds the character in the table and renders it.

But images don't have characters (they might but that's not what builds image) and you can guess what happens when you try to treat image formatted bytes like they were charcters. you get random characters for each file.

If you want to see the strings of a file use strings command.

  • Also good information – NerdOfCode Jun 20 '17 at 20:10
  • 3
    cat does not render anything. that's the job of your shell together with the terminal it is running in. – ohno Jun 21 '17 at 8:20
  • 3
    @ohno the shell also doesn't take part in rendering. It's solely the terminal's job to render the console output of a program. – Ruslan Jun 21 '17 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.